Node.js package published

In my new job I am doing front-end coding with React.js, and every Javascript programmer needs to have some package published at I guess. 😉

So here is my first package: js-object-util

It is inspired by the Immutable API but works for plain Javascript objects. You can use it to easily check for and retrieve properties of deeply nested objects. Let’s have a closer look at the usage:

var objectUtil = require('js-object-util');

// some deeply nested example input
const book = {
	publisher: {
		name: "dpunkt",
		address: {
			city: "Heidelberg",
			plz: 69123
	title : "React",
	year: 2015,
	properties: {
		includes_ebook: false,
		whatever: null,
		dunno: undefined
	readers: [
			type: "user",
			name: "Johnny",
			age: 32,
			nickname: null
			type: "user",
			name: "Brad",
			age: 13,
			nickname: "Bratze"

// let's go
var keyExists;
keyExists = objectUtil.hasIn(book, ['publisher', 'address', 'city']);	    // true
keyExists = objectUtil.hasIn(book, ['publisher', 'address', 'notthere']);   // false
var value;
value = ObjectUtil.getIn(book, ['year']);		   // 2015
value = objectUtil.getIn(book, ['readers', '1', 'name']);  // Brad
value = objectUtil.getIn(book, ['readers', '1', 'notthere'], 'alternate');  // 'alternate'

This is cool because you don’t have to do these stupid step-wise tests for the existence of certain properties anymore.

I hope you like it. See js-object-util for installation instructions. Unit tests and coverage stats are included, the source code is at github. License is MIT.


About dfspspirit

PhD student in bioinformatics, interested in photography, level design, digital image manipulation, architecture and, of course, bioinformatics.
This entry was posted in coding, IT and computers, tools and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s