8000 GitHub - dimsmol/repack: JS objects repackaging tools for node.js
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

dimsmol/repack

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

repacker

Repacks an object to another representation. Useful for preparation of data got from db.

Example:

var img = restruct({
	img: {
		url: 'image_url',
		origSize: {
			x: 'image_orig_width',
			y: 'image_orig_height'
		}
	}
});

var abc = repacker(
	restruct({ name: 'title' }),
	img
);

var result = abc.repack({
	some_field: 1,
	title: 'lala',
	image_url: 'about:blank',
	image_orig_width: 5,
	image_orig_height: 7
});

will produce the following:

{
	someField: 1,
	name: 'lala',
	img: {
		url: 'about:blank',
		origSize: {
			x: 5,
			y: 7
		}
	}
}

getSrcKeys()

Gets src keys corresponding to given dst keys:

abc.getSrcKeys('name', 'img', 'extraField');

will produce

['title', 'image_url', 'image_orig_width', 'image_orig_height', 'extra_field']

Subcomponents can also be used:

abc.getSrcKeys('img.url', 'img.origSize.x');

will produce

['image_url', 'image_orig_width']

reverse()

Performs an action reversed to repack()

abc.reverse({
	extraField: 'extra',
	name: 'lala',
	img: {
		url: 'http://example.com/img.jpg'
	}
}

will produce:

{
	extra_field: 'extra',
	title: 'lala',
	image_url: 'http://example.com/img.jpg'
}

multi

multi maps fields to several repackers, using field prefixes to choose a correct one:

var result = multi({
	'abc': abc,
	'xyz': repacker(
		rename('q', 'w'),
		skip('l')
	),
	'*': repacker()
}).repack({
	abc__title: 'title',
	xyz__q: 5,
	xyz__l: 'l',
	xyz__some_field: 8,
	no_prefix: true
});

will produce the following:

{
	abc: {
		name: 'title'
	},
	xyz: {
		w: 5,
		someField: 8
	},
	noPrefix: true
}

License

MIT

About

JS objects repackaging tools for node.js

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0