🎈 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:
const foo =
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-->
Usage
- Basic usage:
const foo = // return
- Short aliases:
const foo = // return
- ### Aliases table:
Long | Short | Description |
---|---|---|
.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 = // return /*{ wrapper: { inside: { inside: 'of inside' } }}*/
You can create infinite nesting without cluttering the code with a bunch of indents:
const giza = // return /*{ "🧱0": { "🧱1": { "🧱2": { "🧱3": { "🧱4": { "🧱5": { "🧱6": "👁️" } } } } } }}*/
- ### Methods arguments
.key()
or .k()
Argument | Type | Default | Description |
---|---|---|---|
name |
* |
undefined |
Name of the key to install |
depth |
Number |
0 |
Nesting level, don't use it directly |
.val()
or .v()
Argument | Type | Default | Description |
---|---|---|---|
value |
* |
Uses the empty parameter passed during initialization in o() |
Value to assign |
.tab()
or ._()
Argument | Type | Default | Description |
---|---|---|---|
quantity |
Number |
1 |
Tabs quantity, depth nesting level to pass to .key() 's depth parameter |
Advanced usage
- ### Initialization arguments
Argument | Type | Default | Description |
---|---|---|---|
stringify |
Boolean |
false |
Use JSON.stringify() on result object, or not |
space |
String or Number |
undefined |
Separator for stringify, if String use this string as separator, else if Number use number * ' ' as separator |
empty |
* |
undefined |
Replace undefined values with this argument |
Main o()
function accept some arguments to tweak your object, for example:
- Stringify:
const body = // return // {"id":777,"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"}
- Stringify with two spaces:
const body = // return console/*{ "id": 777, "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"}*/
- Placeholder
If you want to pass undefined
value to the key you can:
const foo = // { falsy: undefined }
or
const foo = // { falsy: undefined }
to replace undefined
values with default pass third parameter empty
:
const foo = // { falsy: null }
also you can pass undefined as key:
const foo = // { undefined: "i'm undefined :)" }
to create empty object, just do:
const empty =
⚠️ 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 = // { self: { foo: 'bar' } }
but it's better to use standard syntax:
const weirdo = // { 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.