@flatten an array with child arrays. [1,[2],[3,4],[5,[6,7]]] -> [1,2,3,4,5,[6,7]] The {"deep":true} arg can be provide for deep flattening. [1,[2],[3,4],[5,[6,7]]] -> [1,2,3,4,5,6,7] The original json is returned when the json is not an array.
(json, arg string)
| 3053 | // |
| 3054 | // The original json is returned when the json is not an array. |
| 3055 | func modFlatten(json, arg string) string { |
| 3056 | res := Parse(json) |
| 3057 | if !res.IsArray() { |
| 3058 | return json |
| 3059 | } |
| 3060 | var deep bool |
| 3061 | if arg != "" { |
| 3062 | Parse(arg).ForEach(func(key, value Result) bool { |
| 3063 | if key.String() == "deep" { |
| 3064 | deep = value.Bool() |
| 3065 | } |
| 3066 | return true |
| 3067 | }) |
| 3068 | } |
| 3069 | var out []byte |
| 3070 | out = append(out, '[') |
| 3071 | var idx int |
| 3072 | res.ForEach(func(_, value Result) bool { |
| 3073 | var raw string |
| 3074 | if value.IsArray() { |
| 3075 | if deep { |
| 3076 | raw = unwrap(modFlatten(value.Raw, arg)) |
| 3077 | } else { |
| 3078 | raw = unwrap(value.Raw) |
| 3079 | } |
| 3080 | } else { |
| 3081 | raw = value.Raw |
| 3082 | } |
| 3083 | raw = strings.TrimSpace(raw) |
| 3084 | if len(raw) > 0 { |
| 3085 | if idx > 0 { |
| 3086 | out = append(out, ',') |
| 3087 | } |
| 3088 | out = append(out, raw...) |
| 3089 | idx++ |
| 3090 | } |
| 3091 | return true |
| 3092 | }) |
| 3093 | out = append(out, ']') |
| 3094 | return bytesString(out) |
| 3095 | } |
| 3096 | |
| 3097 | // @keys extracts the keys from an object. |
| 3098 | // |
nothing calls this directly
no test coverage detected
searching dependent graphs…