@nuuf/chink
v0.0.12
Published
Chain objects together parent>child style
Downloads
6
Readme
chink
Chain objects together parent>child style
//#import
import {
create,
add,
remove,
swap,
bindList,
stringifiedFind,
stringifiedReplace,
stringifyList,
parseList
} from '@nuuf/chink';
//#example
const list = [];
bindList(list);
/**
* Hugo
* Stina
* Carl
* Bob
* Henry
* Josie
*
* Anna
* Jacob
*/
add(create(null, 'Hugo'));
add(create(null, 'Anna'));
add(create('any data', 'Stina'), {id: 'Hugo'});
add(create(null, 'Bob'), {id: 'Hugo'});
add(create(null, 'Jacob'), {id: 'Anna'});
add(create(null, 'Henry'), {id: 'Bob'});
add(create(null, 'Josie'), {id: 'Bob'});
add(create({name: 'Carl'}, 'Carl'), {id: 'Stina'});
swap({id: 'Stina'}, {id: 'Anna'});
swap({id: 'Jacob'}, {id: 'Henry'});
// DO NOT swap({id: 'Stina'}, {'Hugo'}) as Stina is a child of Hugo
remove({id: 'Stina'}); // To be crunched
// remove({id: 'Carl'}); ERROR: NOT IN LIST, Carl was a child of Stina
/**
* Hugo
* Anna
* Henry
* Bob
* Jacob
* Josie
* null
*/
// Delete null values
crunchNull();
/**
* Find the entity using find or stringifiedFind if the list is stringified
* Update the data
* stringifiedReplace(entity)
*/
// This
let Josie = find({id: 'Josie'});
// (before stringifiedFind/stringifiedReplace)
stringifyList();
// Or
Josie = stringifiedFind(Josie);
Josie.data = {
name: 'Josie'
};
stringifiedReplace(Josie);
parseList();
console.log(JSON.stringify(list, null, 2));