inline-object
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

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[
    'I believe you have my stapler'
  ]

To this:

name='Milton Waddams' quote[0]='I believe you have my stapler'

Install

npm install inline-object

Usage

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

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.

TypeNumber
Defaultnull

colorize

When true Node's util.inspect is used to colorize by type.

TypeBoolean
Defaultfalse

castTypes

When true and reverting an inline string, values are cast back to their original type if possible. (not full proof).

TypeBoolean
Defaulttrue

transform

TypeNumber
Arguments(key: string, value: any)must return a value.
Defaultnoop

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.
strstring
 
// 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

format

Formats an object resulting in an inline string.

Arguments(obj: object, depth?: number, colorize?: boolean)
Returnsstring

revert

Rerverts a formatted inline string back to an object. Accepts additonal

Arguments(str: string, transform?: (key: string, value: any) => any)
ReturnsT

Examples

Assumes using one of the above import methods of your choice (ES6, Typescript or ES5).

Limiting Depth

const formatted = inlineObj.format({
  name: 'Milton Waddams',
  movie: 'Office Space',
  year: 1999,
  quotes: [
    // will NOT output
  ]
}, 0, true);

Using Transform with Revert

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.
});

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.

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'

Test

npm test

Change

See CHANGE.md

License

See LICENSE.md

Package Sidebar

Install

npm i inline-object

Weekly Downloads

2

Version

1.0.2

License

ISC

Unpacked Size

496 kB

Total Files

36

Last publish

Collaborators

  • origin1tech