@caliatys/object-helper
TypeScript icon, indicating that this package has built-in type declarations

1.0.9 • Public • Published

Build Status Coverage Status

Object Helper

JavaScript helper for object management.

Demo with RunKit

Installation

npm install @caliatys/object-helper --save

Import

JavaScript

let ObjectHelper = require('@caliatys/object-helper').ObjectHelper;

TypeScript

import { ObjectHelper } from '@caliatys/object-helper';

Usage

MergeObjects

var defaultConfig = {
  flipped: false,
  isFixedToCamera: false,
  healthBar: {
    x: 0,
    y: 0
  }
};

var customConfig = {
  isFixedToCamera: true,
  healthBar: {
    y: 10
  }
};

let config = ObjectHelper.mergeObjects(defaultConfig, customConfig);

// {
//   flipped: false,
//   healthBar: { x: 0, y: 10 },
//   isFixedToCamera: true
// }

FilterObjectsByKey

let data = {
  CURSOR_1_UP    : { id: 'up',    key: '1' },
  CURSOR_1_DOWN  : { id: 'down',  key: '2' },
  CURSOR_1_LEFT  : { id: 'left',  key: '3' },
  CURSOR_1_RIGHT : { id: 'right', key: '4' },
  TECH_2_BLOCK   : { id: 'block', key: 'b' },
};

let objects = ObjectHelper.filterObjectsByKey(data, 'CURSOR', ['UP', 'LEFT']);

// [
//   { id: "down",  key: "2" },
//   { id: "right", key: "4" }
// ]

SearchTree

var tree = [{
  title: 'topNode',
  children: [{
    title: 'node1',
    children: [{
      title: 'randomValue1'
    }, {
      title: 'node2',
      children: [{
        title: 'randomValue2',
        children: [{
          title: 'node3',
          children: [{
            title: 'randomValue3'
          }]
        }]
      }]
    }]
  }]
}];

let nodes = ObjectHelper.searchTree(tree, 'children', 'title', 'randomValue2');

// {
//   title: "randomValue2",
//   children: [{
//     title: "node3",
//     children: [{
//        title: 'randomValue3'
//      }]
//   }]
// }

Test

npm run test

Readme

Keywords

none

Package Sidebar

Install

npm i @caliatys/object-helper

Weekly Downloads

2

Version

1.0.9

License

MIT

Unpacked Size

28.5 kB

Total Files

13

Last publish

Collaborators

  • nicolasroehm
  • franzstrudel
  • pkobersi