Formats an object into an inline string using dot notation to indicate level and position in array.
name: 'Milton Waddams',
quotes: [
'I believe you have my stapler'
]
name='Milton Waddams' quote[0]='I believe you have my stapler'
npm install inline-object
Using ES6
import { InlineObject } from 'inline-object';
const inlineObj = new InlineObject();
const formatted = inlineObj.format({
name: 'Milton Waddams',
movie: 'Office Space',
year: 1999
});
Using ES5
var InlineObject = require('inline-object').InlineObject;
var inlineObj = new InlineObject();
var formatted = inlineObj.format({
name: 'Milton Waddams',
movie: 'Office Space',
year: 1999
});
The Result
name='Milton Waddams' movie='Office Space' year=1999
Options can be set in the constructor or through the instantiated instance.
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 |
When true Node's util.inspect is used to colorize by type.
Type | Boolean |
Default | false |
When true and reverting an inline string, values are cast back to their original type if possible. (not full proof).
Type | Boolean |
Default | true |
Type | Number | |
Arguments | (key: string, value: any) | must return a value. |
Default | noop |
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 optional
transform?: (key: string, value: any) => any
Formats an object resulting in an inline string.
Arguments | (obj: object, depth?: number, colorize?: boolean) |
Returns | string |
Rerverts a formatted inline string back to an object. Accepts additonal
Arguments | (str: string, transform?: (key: string, value: any) => any) |
Returns | T |
Assumes using one of the above import methods of your choice (ES6, Typescript or ES5).
const formatted = inlineObj.format({
name: 'Milton Waddams',
movie: 'Office Space',
year: 1999,
quotes: [
// will NOT output
]
}, 0, true);
const str = "name='Milton Waddams' movie='Office Space' year=1999";
const reverted = inlineObj.revert(str, function (k, v) {
const float = parseFloat(v); // parse out float/numbers
if (isNaN(float)) // if not a number return orig. string.
return v;
return float; // otherwise return the parsed float/number.
});
The output follows the same naming convention as lodash's get/set methods. If you are familiar with those this will make sense.
const formatted = inlineObj.format({
name: 'Milton Waddams',
movie: 'Office Space',
year: 1999,
quotes: [
'I believe you have my stapler'
]
});
Results in
name='Milton Waddams' movie='Office Space' year=1999 quote[0]='I believe you have my stapler'
npm test
See CHANGE.md
See LICENSE.md