2.0.0 • Published 3 years ago

densen v2.0.0

Weekly downloads
1
License
MIT
Repository
-
Last release
3 years ago

densen

Reduce the size of your serialized data with densen by transforming object literal to array of values.

Examples

With Object

const densen = require('densen');

const unflattened = {
	name: 'Alexander',
	age: 32,
	profession: {
		title: 'engineer',
		salary: 70000,
	},
	hobbies: [
		{
			activity_name: 'soccer',
			hours_per_week: 4,
		},
		{
			activity_name: 'piano',
			hours_per_week: 3,
		},
		{
			activity_name: 'painting',
			hours_per_week: 2,
		},
	],
	qualities: ['perseverant', 'open-minded', 'funny'],
};

const flattened = [
	'Alexander',
	32,
	['engineer', 70000],
	[
		['soccer', 4],
		['piano', 3],
		['painting', 2],
	],
	['perseverant', 'open-minded', 'funny'],
];

const shape = {
	name: '',
	age: 0,
	profession: {
		title: '',
		salary: 0,
	},
	hobbies: [
		{
			activity_name: '',
			hours_per_week: 0,
		},
	],
	qualities: [''],
};

const compressor = densen(shape);

compressor.unflatten(flattened); // returns unflattened
compressor.flatten(unflattened); // returns flattened

With Array

const densen = require('densen');

const unflattened = [
	{
		name: 'Alice',
		age: 44,
	},
	{
		name: 'Bob',
		age: 32,
	},
];

const flattened = [
	['Alice', 44],
	['Bob', 32],
];

const shape = [
	{
		name: '',
		age: 0,
	},
];

const compressor = densen(shape);

compressor.unflatten(flattened); // returns unflattened
compressor.flatten(unflattened); // returns flattened

Functions

unflatten = values => keys / values

flatten = keys / values => values

Caution

The order of keys in a literal object does NOT (always) follow the insertion order, except if one only use alphabetic keys. stackoverflow post reference

Examples

Good shape

const goodShape = {
	name: '', // good
	age: 0, // good
};

Bad Shape

const badShape = {
	att: 0, // good
	'1' : '', // bad
	0 : '', // bad
	Symbol(): '' // bad
};

License

MIT License

Copyright (c) 2021 CreatifCreateur