1.0.0 β€’ Published 6 years ago

obdo v1.0.0

Weekly downloads
4
License
MIT
Repository
gitlab
Last release
6 years ago

🎈 obdo

Simple Object generator.

Tired of the json? Rethink it with obdo, (simple object dot notation).

The obdo package will allow you to avoid ♾️ infinitely long levels of nesting and indentation, as well as make the markup of objects more intuitive and cleaner.

Instead of:

const foo = {
  bar: 'baz',
  qux: {
    quux: 0,
    quuz: {
      corge: 1
    }
  }
}

You can:

import o from 'obdo'

const foo = o()
  .k('bar').v('baz')
  .k('qux')
  ._().k('quux').v(0)
  ._().k('quuz')
  ._(2).k('corge').v(1)
  .o()

Simple, isn't it? 🌞

Install

npm i obdo

Works in node and modern browsers.

In browser you can import it like es module:

Just copy index.js from this repo, rename it to obdo.js and use:

<!--index.html-->
<script type="module">
  import o from './obdo.js'
  console.log(o().k('hello').v('world').o())
</script>

Usage

  • Basic usage:
const foo = o()
  .key('bar').val('baz')
  .key('qux')
  .tab().key('quux').val('quuz')
  .o() // return
  • Short aliases:
const foo = o()
  .k('bar').v('baz')
  .k('qux')
  ._().k('quux').v('quuz')
  .o() // return
  • Aliases table:

LongShortDescription
.key().k()Create new key
.val().v()Assign value to key
.tab()._()Put next key inside one of the previous
.obj().o()Return resulted object
  • Nesting

To created nested object, increase .tab() argument. By default it equals to 1:

const nested = o()
  .k('wrapper')
  ._().k('inside')
  ._(2).k('inside').v('of inside')
  .o() // return

/*
{
  wrapper: {
    inside: {
      inside: 'of inside'
    }
  }
}
*/

You can create infinite nesting without cluttering the code with a bunch of indents:

const giza = o(true, '  ')
  .k('🧱0')
  ._(1).k('🧱1')
  ._(2).k('🧱2')
  ._(3).k('🧱3')
  ._(4).k('🧱4')
  ._(5).k('🧱5')
  ._(6).k('🧱6').v('πŸ‘οΈ')
  .o() // return

/*
{ 
  "🧱0": {
    "🧱1": {
      "🧱2": {
        "🧱3": {
          "🧱4": {
            "🧱5": {
              "🧱6": "πŸ‘οΈ"
            }
          }
        }
      }
    }
  }
}
*/
  • Methods arguments

.key() or .k()

ArgumentTypeDefaultDescription
name*undefinedName of the key to install
depthNumber0Nesting level, don't use it directly

.val() or .v()

ArgumentTypeDefaultDescription
value*Uses the empty parameter passed during initialization in o()Value to assign

.tab() or ._()

ArgumentTypeDefaultDescription
quantityNumber1Tabs quantity, depth nesting level to pass to .key()'s depth parameter

Advanced usage

  • Initialization arguments

ArgumentTypeDefaultDescription
stringifyBooleanfalseUse JSON.stringify() on result object, or not
spaceString or NumberundefinedSeparator for stringify, if String use this string as separator, else if Number use number * ' ' as separator
empty*undefinedReplace undefined values with this argument

Main o() function accept some arguments to tweak your object, for example:

  • Stringify:
const body = o(true)
  .k('id').v(777)
  .k('token').v('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9')
  .o() // return

// {"id":777,"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"}
  • Stringify with two spaces:
const body = o(true, '  ')
  .k('id').v(777)
  .k('token').v('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9')
  .o() // return

console.log(body)
/*
{
  "id": 777,
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"
}
*/
  • Placeholder

If you want to pass undefined value to the key you can:

const foo = o().k('falsy')
// { falsy: undefined }

or

const foo = o().k('falsy').v(undefined)
// { falsy: undefined }

to replace undefined values with default pass third parameter empty:

const foo = o(false, '', null).k('falsy')
// { falsy: null }

also you can pass undefined as key:

const foo = o().v("i'm undefined :)").o()
// { undefined: "i'm undefined :)" }

to create empty object, just do:

const empty = o().o()

⚠️ Remember that you should always call the .o() method at the end of any chain to return the desired object.

.val() argument

The argument can be any expression, any type, you can even write something like this:

const weirdo = o()
  .k('self').v(o().k('foo').v('bar').o())
  .o()

// { self: { foo: 'bar' } }

but it's better to use standard syntax:

const weirdo = o()
  .k('self')
  ._().k('foo').v('bar')
  .o()

// { self: { foo: 'bar' } }

Contribute

If you want to help, or found a bug - open issue, merge/pull request, or donate πŸ€‘.

License

Licensed under the MIT license.

1.0.0

6 years ago

0.1.0

6 years ago

0.0.1

6 years ago