2.2.0 • Published 6 years ago

structured-json v2.2.0

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

Structured JSON

Operators that make complex JSON structures easy to read and write.

ActionOperatorKey/Value
Assign Value<=Value
Assign Defaults<<, >>Key
Merge<<, >>Value
Mixin$Key
Conditional Defaults<<?, >>?Key

Use the update function for immutable updates.

Install

npm install structured-json

Import

import { build, update } from "structured-json"

Assign

let { stores, products } = build({
  "stores": {
    "grocery": {
      "products": "<= products"
    }
  },
  "products": {
    "milk": {
      "store": "<= stores.grocery"
    }
  }
})

stores.grocery.products // { milk }
products.milk.store     // { products }

Assignment supports circular references, but it is up to you to be careful about infinite enumeration.

Merge

let { products } = build({
  "organicProducts": {
    "eggs": {},
    "milk": {}
  },
  "veganProducts": {
    "kale": {},
    "tofu": {}
  },
  "products": "<= organicProducts << veganProducts"
})

products // { eggs: {},
         //   milk: {},
         //   kale: {},
         //   tofu: {} }

Defaults

When used in a key, the merge operator defines a default object for its siblings (>>) or its parent (<<):

let { organicProducts, veganProducts } = build({
  "organicProducts": {
    ">>": { "organic": true },
    "eggs": {},
    "milk": {}
  },
  "veganProducts": {
    ">>": { "vegan": true },
    "kale": {},
    "tofu": {}
  }
})

organicProducts // { eggs: { organic },
                //   milk: { organic } }
veganProducts   // { kale: { vegan },
                //   tofu: { vegan } }

Define defaults for sibling child objects with successive merge operators (">> >>":).

Mixin

A mixin is a variable meant only for referencing, and does not show up in enumeration.

let { products } = build({
  "products": {
    "$green": {
      "color": "green"
    },
    "$white": {
      "color": "white"
    },
    "milk": { "<<": "$white" },
    "kale": { "<<": "$green" },
    "tofu": { "<<": "$white" }
  }
})

products // { milk: { color: "white" },
         //   kale: { color: "green" },
         //   tofu: { color: "white" } }

Conditionals

let { products } = build({
  "winter": true,
  "products": {
    ">>? winter": {
      "local": false
    },
    ">>": {
      "local": true
    },
    "kale": {}
  }
})

products // { kale: { local: false } }

Update

let config = build(...)

config = update(config,
  "products.lettuce <<", { "<<": "$green" }  
)

The update function uses immutability-helper to create entirely new objects for each update.

2.2.0

6 years ago

2.1.0

6 years ago

2.0.0

6 years ago

1.3.0

6 years ago

1.2.0

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago