Inline Object
Formats an object into an inline string using dot notation to indicate level and position in array.
Converts this:
name: 'Milton Waddams', quotes:
To this:
name='Milton Waddams' quote[0]='I believe you have my stapler'
Install
npm install inline-object
Usage
Using ES6
;; ;
Using ES5
;; ;
The Result
name='Milton Waddams' movie='Office Space' year=1999
Options
Options can be set in the constructor or through the instantiated instance.
depth
The max depth to parse when converting an object to inline string. When null allows any depth with 0 being the top level.
Type | Number |
Default | null |
colorize
When true Node's util.inspect is used to colorize by type.
Type | Boolean |
Default | false |
castTypes
When true and reverting an inline string, values are cast back to their original type if possible. (not full proof).
Type | Boolean |
Default | true |
transform
Type | Number | |
Arguments | (key: string, value: any) | must return a value. |
Default | noop |
API
Inline object api methods. If you are familiar with Typescript the below annotations will look familiar. For those who are not you can ignore said annotaitons. They merely indicate the "types" for method arguments and return values. If you are NOT using Typescript you can completely ignore them. The module will work as expected without using typings.
For example the revert arguments indicate the following:
// indicates expects a string.str: string // Indicates the method expects a key which is a string,// a value of any type and it expects a returned value of// any type. The "?" simply indicates that the argument is optionaltransform?:any
format
Formats an object resulting in an inline string.
Arguments | (obj: object, depth?: number, colorize?: boolean) |
Returns | string |
revert
Rerverts a formatted inline string back to an object. Accepts additonal
Arguments | (str: string, transform?: (key: string, value: any) => any) |
Returns | T |
Examples
Assumes using one of the above import methods of your choice (ES6, Typescript or ES5).
Limiting Depth
;
Using Transform with Revert
;;
Nested Output
The output follows the same naming convention as lodash's get/set methods. If you are familiar with those this will make sense.
;
Results in
name='Milton Waddams' movie='Office Space' year=1999 quote[0]='I believe you have my stapler'
Test
npm test
Change
See CHANGE.md
License
See LICENSE.md