Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/nicolasroehm/objecthelper

JavaScript helper for object management
https://github.com/nicolasroehm/objecthelper

javascript typescript

Last synced: about 4 hours ago
JSON representation

JavaScript helper for object management

Awesome Lists containing this project

README

        

[![Build Status](https://travis-ci.org/Caliatys/ObjectHelper.svg?branch=master)](https://travis-ci.org/Caliatys/ObjectHelper)
[![Coverage Status](https://coveralls.io/repos/github/Caliatys/ObjectHelper/badge.svg?branch=master)](https://coveralls.io/github/Caliatys/ObjectHelper?branch=master)

# Object Helper
> JavaScript helper for object management.

Demo with RunKit

## Installation
```sh
npm install @caliatys/object-helper --save
```



## Import
### JavaScript
```javascript
let ObjectHelper = require('@caliatys/object-helper').ObjectHelper;
```

### TypeScript
```typescript
import { ObjectHelper } from '@caliatys/object-helper';
```

## Usage

### MergeObjects
```javascript
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
```javascript
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
```javascript
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
```sh
npm run test
```