JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 90
  • Score
    100M100P100Q54483F
  • License MIT

JSON-encoded operations on JSON

Package Exports

  • jsonop

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (jsonop) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

jsonop

JSON-encoded operations on JSON

It’s like $.extend(), but much more powerful; by assigning special meanings to the value null and the property name "_" (a single underscore) complex operations can be represented intuitively using JSON.

Installation and usage

$ npm install jsonop
jsonop = require("jsonop");

Object Operations

1. Recursive merge (extend)

    jsonop({ a: 3, b: { c: 4 }}, { a: 5, b: { d: 6 }})
    // Returns { a: 5, b: { c: 4, d: 6 }}

2. Delete keys with <key>: null

  jsonop({ a: 3, b: 4 }, { a: null });
  // Returns { b: 4 }

3. Replace a subtree with { _: null, … }

    jsonop({ a: 3, b: { c: 4 }}, { a: 5, b: { _: null, d: 6 }})
    // Returns { a: 5, b: { d: 6 }}

Array Operations

4. Replace array

    jsonop({ a: 3, b: [1, 2, 3]}, { b: [4, 5]})
    // Returns { a: 3, b: [4, 5]}

Array as List

5. Append items with [null, …] Creates the array if it doesn’t exist.

    jsonop({ a: 3, b: [1, 2, 3]}, { b: [null, 4, 5]})
    // Returns { a: 3, b: [1, 2, 3, 4, 5]}
    
    jsonop({ a: 3 }, { b: [null, 4, 5]})
    // Returns { a: 3, b: [4, 5]}

6. Prepend items with […, null] Creates the array if it doesn’t exist.

    jsonop({ a: 3, b: [1, 2, 3]}, { b: [4, 5, null]})
    // Returns { a: 3, b: [4, 5, 1, 2, 3]}

7. Splice with { _: [<index>, <remove_count>, <insert_items> ]} Remove or insert items at a specific index. Spec under review; Not implemented.

    jsonop({ a: 3, b: [1, 2, 3]}, { b: { _: [1, 1, 4, 5]}})
    // Returns { a: 3, b: [1, 4, 5, 3]}

Array as Set

Set operations only work correctly when items are primitives (strings or numbers).

8. Add items with [null, …, null] Creates the array if it doesn’t exist. Skips duplicates.

    jsonop({ a: 3, b: [1, 2, 3]}, { b: [null, 3, 5, null]})
    // Returns { a: 3, b: [4, 5, 1, 2, 3]}

9. Remove items with { _: <item> } Not yet implemented.

    jsonop({ a: 3, b: [1, 2, 3]}, { b: { _: 3 }})
    // Returns { a: 3, b: [1, 2]}

Array as Tuple

10. Replace items with { <index>: <item> } Not yet implemented.

    jsonop({ a: 3, b: [1, 2, 3]}, { b: { _: 3 }})
    // Returns { a: 3, b: [1, 2]}

Limitations

  • null cannot be used
  • "_" (a single underscore) cannot be used as a property name
  • Sets cannot contain objects or arrays