JSPM

  • Created
  • Published
  • Downloads 108
  • Score
    100M100P100Q91584F
  • License MIT

Add missing keys into plain objects, according to a provided schema object.

Package Exports

  • object-fill-missing-keys

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 (object-fill-missing-keys) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

object-fill-missing-keys

Standard JavaScript

Add missing keys into plain objects, according to a provided schema object

Build Status Coverage Status bitHound Overall Score bitHound Dependencies bitHound Dev Dependencies Downloads/Month

Install

$ npm install --save object-fill-missing-keys

Purpose

Imagine you parsed two JSON files and got two objects. Each has few keys that another one doesn't. How do you make so that each has the same set of keys, in alphabetical order? I call that normalising. Mind you; each JSON file can be nested spaghetti of plain objects within arrays within plain objects and contain gazillion of string values too. Normalising involves Abstract Syntax Tree (AST) traversal, what requires recursive operations.

This library performs this normalising part. Feed an input object and a schema object and it will fill any keys that are in the schema but not in the input.

Now, you may ask, how do you generate a schema object?

Flatten and merge all arrays, then set all key values of it to the default value you want schema to bear.

For that you'll need few other libraries:

But let's get back to the main subject.

Use

var fillMissingKeys = require('object-fill-missing-keys')
var f = fillMissingKeys(
  {
    b: 'b'
  },
  {
    a: false,
    b: false,
    c: false
  }
)
console.log('f = ' + JSON.stringify(f, null, 4))
// => {
//      a: false,
//      b: 'b',
//      c: false
//    }

API

fillMissingKeys(incompleteObj, schemaObj)

API - Input

Input argument Type Obligatory? Description
incompleteObj Plain object yes Plain object. Can have nested values.
schemaObj Plain object yes Schema object which contains a desired set of values. Can be nested or hold arrays of things.

Testing

$ npm test

For unit tests we use AVA, Istanbul CLI and JS Standard notation.

Contributing

All contributions are welcome. Please stick to Standard JavaScript notation and supplement the test.js with new unit tests covering your feature(s).

If you see anything incorrect whatsoever, do raise an issue. If you file a pull request, I'll do my best to help you to get it merged promptly. If you have any comments on the code, including ideas how to improve things, don't hesitate to contact me by email.

Licence

MIT License (MIT)

Copyright (c) 2016 Code and Send Ltd, Roy Reveltas

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.