@ngyv/prop-utils

Utility functions for properties management

Stats

stars 🌟issues ⚠️updated 🛠created 🐣size 🏋️‍♀️
00May 10, 2018Apr 1, 2018Minified + gzip package size for @ngyv/prop-utils in KB

Readme

prop-utils Build Status codecov npm download

Utility functions for properties management

Install

$ npm i @ngyv/prop-utils --save

Usage

import {
  types,
  getPropertyTypeName,
  identify,
  compareType,
  isEqual,
  parseValueToType,
  parseJson,
} from '@ngyv/prop-utils'

const propertyA = {
  name: 'Siti Nurhaliza',
  age: undefined,
};

const propertyB = {
  name: 'Siti Nurhaliza',
  age: 21,
};

identify(propertyA.age) === types.undefined;
//=> true

getPropertyTypeName(propertyA.name);
//=> 'string'

compareType(propertyA, propertyB);
//=> true

const similar = { nil: [types.undefined, types.null] };
compareType(undefined, null, similar);
//=> true

class Banana {
  shout() {
    return 'HELLO!';
  }
}
class B1 extends Banana {}
class B2 extends Banana {}
const b1 = new B1();
const b2 = new B2();
compareValue(b1, b2);
//=> false

compareValue(b1, b2, { classComparator: (b1, b2) => return b1.shout() === b2.shout() });
//=> true

comparePropertyToType('str', types.null);
//=> false

const acceptedTypes = { strings: [types.string, types.null, types.emptyString] };
comparePropertyToType('str', types.null, acceptedTypes);
//=> true

isEqual(propertyA, propertyB);
//=> false

parseValueToType('12.1', types.number);
//=> 12.1

parseValueToType(null, types.number);
//=> null

const json = {
  accountId: 1,
  createdAt: '2018-04-02T09:12:20.221Z',
}
const expectedTypes = {
  accountId: types.number,
  createdAt: types.date,
}

parseJson(json, expectedTypes);
//=> { accountId: 1, createdAt: Date 2018-04-02 09:12:20 221ms Z }

License

MIT © Yvonne Ng

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.