Skip to content

Commit

Permalink
♻️ refactor: update comment for all functions #2
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Oct 27, 2024
1 parent c50f4d8 commit e846bef
Show file tree
Hide file tree
Showing 6 changed files with 307 additions and 398 deletions.
63 changes: 35 additions & 28 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ var _json = jsonI.ConfigCompatibleWithStandardLibrary
// during marshalling, it returns the error.
//
// Parameters:
// - `v`: The Go value to be marshalled into JSON.
// - `v`: The Go value to be marshalled into JSON.
//
// Returns:
// - A byte slice containing the JSON representation of the input value.
// - An error if the marshalling fails.
// - A byte slice containing the JSON representation of the input value.
// - An error if the marshalling fails.
//
// Example:
// jsonData, err := Marshal(myStruct)
//
// jsonData, err := Marshal(myStruct)
func Marshal(v interface{}) ([]byte, error) {
return _json.Marshal(v)
}
Expand All @@ -32,16 +33,17 @@ func Marshal(v interface{}) ([]byte, error) {
// It returns the resulting JSON byte slice or an error if marshalling fails.
//
// Parameters:
// - `v`: The Go value to be marshalled into JSON.
// - `prefix`: A string that will be prefixed to each line of the output JSON.
// - `indent`: A string used for indentation (typically a series of spaces or a tab).
// - `v`: The Go value to be marshalled into JSON.
// - `prefix`: A string that will be prefixed to each line of the output JSON.
// - `indent`: A string used for indentation (typically a series of spaces or a tab).
//
// Returns:
// - A byte slice containing the formatted JSON representation of the input value.
// - An error if the marshalling fails.
// - A byte slice containing the formatted JSON representation of the input value.
// - An error if the marshalling fails.
//
// Example:
// jsonIndented, err := MarshalIndent(myStruct, "", " ")
//
// jsonIndented, err := MarshalIndent(myStruct, "", " ")
func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
return _json.MarshalIndent(v, prefix, indent)
}
Expand All @@ -53,14 +55,15 @@ func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
// JSON string. If an error occurs during the process, it returns an error.
//
// Parameters:
// - `v`: The Go value to be marshalled into JSON.
// - `v`: The Go value to be marshalled into JSON.
//
// Returns:
// - A string containing the JSON representation of the input value.
// - An error if the marshalling fails.
// - A string containing the JSON representation of the input value.
// - An error if the marshalling fails.
//
// Example:
// jsonString, err := MarshalToString(myStruct)
//
// jsonString, err := MarshalToString(myStruct)
func MarshalToString(v interface{}) (string, error) {
return _json.MarshalToString(v)
}
Expand All @@ -72,14 +75,15 @@ func MarshalToString(v interface{}) (string, error) {
// is successful, it populates the value `v`. If an error occurs, it returns the error.
//
// Parameters:
// - `data`: A byte slice containing JSON data to be unmarshalled.
// - `v`: A pointer to the Go value where the unmarshalled data will be stored.
// - `data`: A byte slice containing JSON data to be unmarshalled.
// - `v`: A pointer to the Go value where the unmarshalled data will be stored.
//
// Returns:
// - An error if the unmarshalling fails.
// - An error if the unmarshalling fails.
//
// Example:
// err := Unmarshal(jsonData, &myStruct)
//
// err := Unmarshal(jsonData, &myStruct)
func Unmarshal(data []byte, v interface{}) error {
return _json.Unmarshal(data, v)
}
Expand All @@ -91,14 +95,15 @@ func Unmarshal(data []byte, v interface{}) error {
// successful, it populates the value `v`. If an error occurs, it returns the error.
//
// Parameters:
// - `str`: A string containing JSON data to be unmarshalled.
// - `v`: A pointer to the Go value where the unmarshalled data will be stored.
// - `str`: A string containing JSON data to be unmarshalled.
// - `v`: A pointer to the Go value where the unmarshalled data will be stored.
//
// Returns:
// - An error if the unmarshalling fails.
// - An error if the unmarshalling fails.
//
// Example:
// err := UnmarshalFromString(jsonString, &myStruct)
//
// err := UnmarshalFromString(jsonString, &myStruct)
func UnmarshalFromString(str string, v interface{}) error {
return _json.UnmarshalFromString(str, v)
}
Expand All @@ -110,13 +115,14 @@ func UnmarshalFromString(str string, v interface{}) error {
// MarshalToString function. If an error occurs during marshalling, it returns an empty string.
//
// Parameters:
// - `data`: The Go value to be converted to JSON, or a string to be returned directly.
// - `data`: The Go value to be converted to JSON, or a string to be returned directly.
//
// Returns:
// - A string containing the JSON representation of the input value, or an empty string if an error occurs.
// - A string containing the JSON representation of the input value, or an empty string if an error occurs.
//
// Example:
// jsonStr := Json(myStruct)
//
// jsonStr := Json(myStruct)
func Json(data interface{}) string {
s, ok := data.(string)
if ok {
Expand All @@ -136,13 +142,14 @@ func Json(data interface{}) string {
// the MarshalIndent function. If an error occurs during marshalling, it returns an empty string.
//
// Parameters:
// - `data`: The Go value to be converted to pretty-printed JSON, or a string to be returned directly.
// - `data`: The Go value to be converted to pretty-printed JSON, or a string to be returned directly.
//
// Returns:
// - A string containing the pretty-printed JSON representation of the input value, or an empty string if an error occurs.
// - A string containing the pretty-printed JSON representation of the input value, or an empty string if an error occurs.
//
// Example:
// jsonPrettyStr := JsonPretty(myStruct)
//
// jsonPrettyStr := JsonPretty(myStruct)
func JsonPretty(data interface{}) string {
s, ok := data.(string)
if ok {
Expand Down
18 changes: 9 additions & 9 deletions normalization.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,33 @@ package unify4g
// more common ASCII equivalents. The specific transformations are defined in a switch statement that
// maps each rune to its normalized string value. This includes:
//
// - Ligatures (e.g., 'Æ' to "AE", 'Œ' to "OE")
// - Special characters (e.g., 'ß' to "ss")
// - Accented characters (e.g., 'é' to "e", 'ç' to "c")
// - Various other characters with diacritics are converted to their closest ASCII equivalent.
// - Ligatures (e.g., 'Æ' to "AE", 'Œ' to "OE")
// - Special characters (e.g., 'ß' to "ss")
// - Accented characters (e.g., 'é' to "e", 'ç' to "c")
// - Various other characters with diacritics are converted to their closest ASCII equivalent.
//
// If the input rune does not match any of the defined cases, the function returns the rune as a string,
// preserving its original representation.
//
// Parameters:
// - `r`: A rune representing a Unicode character that may need normalization.
// - `r`: A rune representing a Unicode character that may need normalization.
//
// Returns:
// - A string that represents the normalized form of the input rune. This string is typically ASCII,
// making it easier to handle in contexts where only basic characters are expected.
//
// Example:
//
// normalized := normalize_rune('É')
// fmt.Println(normalized) // Output: "E"
// normalized := normalize_rune('É')
// fmt.Println(normalized) // Output: "E"
//
// Notes:
// - This function is particularly useful for text processing, sanitization, or when preparing data
// for systems that require ASCII-compatible strings.
//
// Replacements are taken from
// - https://github.com/sindresorhus/slugify/blob/master/replacements.js
// - https://github.com/cocur/slugify/tree/master/Resources/rules
// - https://github.com/sindresorhus/slugify/blob/master/replacements.js
// - https://github.com/cocur/slugify/tree/master/Resources/rules
func normalize_rune(r rune) string {
switch r {
case 'Ə':
Expand Down
27 changes: 13 additions & 14 deletions random.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ func init() {
// r.Intn(max-min+1) + min, where `r` is a custom random generator that has been initialized elsewhere in the code.
//
// Parameters:
// - `min`: The lower bound of the random number range (inclusive).
// - `max`: The upper bound of the random number range (inclusive).
// - `min`: The lower bound of the random number range (inclusive).
// - `max`: The upper bound of the random number range (inclusive).
//
// Returns:
// - A random integer between `min` and `max`, including both bounds.
// - A random integer between `min` and `max`, including both bounds.
//
// Example:
//
// randomNum := NextInt(1, 10)
// fmt.Println("Random number between 1 and 10:", randomNum)
// randomNum := NextInt(1, 10)
// fmt.Println("Random number between 1 and 10:", randomNum)
func NextInt(min, max int) int {
if min >= max {
return min // or handle the error appropriately
Expand All @@ -57,21 +57,20 @@ func NextInt(min, max int) int {
// in the result by using the formula: r.Intn(max-min+1) + min.
//
// Parameters:
// - `min`: The lower bound of the random number range (inclusive).
// - `max`: The upper bound of the random number range (inclusive).
// - `min`: The lower bound of the random number range (inclusive).
// - `max`: The upper bound of the random number range (inclusive).
//
// Returns:
// - A random integer between `min` and `max`, including both bounds.
// - A random integer between `min` and `max`, including both bounds.
//
// Example:
//
// randomNum := NextReseed(1, 10)
// fmt.Println("Random number between 1 and 10 after reseeding:", randomNum)
// randomNum := NextReseed(1, 10)
// fmt.Println("Random number between 1 and 10 after reseeding:", randomNum)
func NextReseed(min, max int) int {
// Reseed the custom random generator with a new seed
x := time.Now().UTC().UnixNano() + int64(r.Int())
r.Seed(x)

// Ensure max is included in the range
if min >= max {
return min
Expand All @@ -88,12 +87,12 @@ func NextReseed(min, max int) int {
// It abstracts away the error handling by returning an empty string in case of failure.
//
// Returns:
// - A string representing the newly generated UUID.
// - An empty string if an error occurs during UUID generation.
// - A string representing the newly generated UUID.
// - An empty string if an error occurs during UUID generation.
//
// Example:
//
// uuid := NextUUID()
// uuid := NextUUID()
//
// if uuid == "" {
// fmt.Println("Failed to generate UUID")
Expand Down
22 changes: 10 additions & 12 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,29 @@ import "reflect"
// IsPrimitive checks whether the given value is a primitive type in Go.
//
// Primitive types include:
// - Signed integers: int, int8, int16, int32, int64
// - Unsigned integers: uint, uint8, uint16, uint32, uint64, uintptr
// - Floating-point numbers: float32, float64
// - Complex numbers: complex64, complex128
// - Boolean: bool
// - String: string
// - Signed integers: int, int8, int16, int32, int64
// - Unsigned integers: uint, uint8, uint16, uint32, uint64, uintptr
// - Floating-point numbers: float32, float64
// - Complex numbers: complex64, complex128
// - Boolean: bool
// - String: string
//
// The function first checks if the input value is `nil`, returning `false` if so. Otherwise, it uses reflection to determine
// the type of the value and compares it against known primitive types.
//
// Parameters:
// - `value`: An interface{} that can hold any Go value. The function checks the type of this value.
// - `value`: An interface{} that can hold any Go value. The function checks the type of this value.
//
// Returns:
// - `true` if the value is of a primitive type.
// - `false` if the value is `nil` or not a primitive type.
// - `true` if the value is of a primitive type.
// - `false` if the value is `nil` or not a primitive type.
//
// Example:
//
// primitive := 42
//
// primitive := 42
// if IsPrimitive(primitive) {
// fmt.Println("The value is a primitive type.")
// } else {
//
// fmt.Println("The value is not a primitive type.")
// }
func IsPrimitive(value interface{}) bool {
Expand Down
Loading

0 comments on commit e846bef

Please sign in to comment.