Q-Set
Set query string style fields on an object.
Installation
npm install q-set
;
API
qset.deep(obj: any, path: string, val: any): any
- Sets nested paths such as
a[b][c]
. - Concats duplicated properties on an object as an array.
- Supports
[]
as array push and will be appended to an existing array or create a new one.
// Set a key.; //-> { a: 1 } // Set a nested path.; //-> { a: { b: 1 } } // Implicit array creation (keys used multiple times).const obj = {};; //-> { a: 1 }; //-> { a: [1, 2] } // Explicit array creation.const obj = {};; //-> { a: [1] }; //-> { a: [1, 2] } // Will also automatically create an array when the a key is a positive integer.const obj = {};; //-> { a: [1] }; //-> { a: [1, 2] }; //-> { b: [,,3] } // Nested array creation.const obj = {};; //-> { a: [{ b: 1 }] }; //-> { a: [{ b: 1 }, { b: 2 }] }
qset.shallow(obj: any, path: string, val: any): any
- Concats duplicated properties on an object as an array.
- Will not follow nested query strings.
- If
[]
is used it will be converted to an explicit index and flattened.
// Doesn't unflatten qs syntax but does append to arrays.const obj = {};; //-> { "a[1]": 1 }; //-> { "a[1]": [1, 2] } // Automatically converts array push "[]" to indexes.const obj = {};; //-> { "a[0]": 1 }; //-> { "a[0]": 1, "a[1]": 2 }
Contributions
- Use
npm test
to build and run tests.
Please feel free to create a PR!