obj-format
v2.0.0
Published
Creates a formatting template to parse collections with null-safe variables, functions, and ignores. Useful for 3rd party API formatting.
Downloads
5
Maintainers
Readme
Object Formatter
Of course your objects are formatted nicely internally. You wrote the code, and you're a genius. But wait! Evil 3rd party API says your objects are crap, and they want them formatted THE RIGHT (their) WAY!
Never fear, Object Formatter™ is here!
Show me how to use it already!
Installation
$ npm install obj-format
Creating a Formatter instance:
const Formatter = require('object-formatter');
/* Note: You can totally store your maps as JSON files and require them.
* Like cloth diapers, JSON mapping files are nice, tidy, and reusable! */
const destMap = {
// Value is replaced by variable and ignored from object search
project_name: "!$1 is great!",
// Search property is replaced with supplied variables
"project_id": "$2.id",
"first_name": "person.first",
"last_name": "person.last",
"address": {
"geo": {
"lat": "person.address.geo.lat"
}
},
// Value ignored by parser
"!ip_address": "127.0.0.1"
}
const THIRD_PARTY_FORMAT = new Formatter(destMap);
Default Behavior
Properties are not parsed by the object-looker-upper (elvis operator).
Values are parsed by the object-looker-upper (Really? Can't think of better name?).
Any property or value containing a $n
will be replaced with its corresponding variable in the array you pass before property lookup.
Negating default behavior
Maybe you want a property to be a value from a source object, or maybe you want a constant string as a value. Just prefix your property or value with !
and the default behavior will be reversed!
Using the formatter
THIRD_PARTY_FORMAT.format({
person: {
first: 'John',
last: 'Zoidberg',
address: {
street: '1234 Example Blvd',
geo: {
lat: 1.2345
lng: -1.2345
}
}
},
finance: {
id: 123,
name: 'Finance',
date: new Date()
}
}, ['Finance', 'finance']);
/*
Returns:
{
"project_name": "Finance is great!",
"project_id": "123",
"first_name": "John",
"last_name": "Zoidberg",
"address": {
"geo": {
"lat": "1.2345"
}
},
"ip_address": "127.0.0.1"
}
*/
Methods
format(obj, vars)
instance method
Populates a new copy of the mapping object formatted data
Arguments
obj
- Source object to be parsed. Should match values in the destination object supplied in the constructorvars
- (Optional) Array of variables to be replaced in matching strings (I.E.['foo','bar'] + "$2.$1-$1"
=>"bar.foo-foo"
)
Returns
Destination object populated with source values
get(props, obj)
class method
Class method. Gets the property array or dot-notated field from an object. Null safe.
Arguments
props
Array of properties in nested order or dot notated string (I.E.["foo", "bar", "baz"]
OR"foo.bar.baz"
)obj
Source object to search.
Returns
Value found from search or undefined.
Worried about undefined
? Worry no longer!
Object Formatter takes all the worry out of property lookups. You're welcome.
Problems? Issues? Rude remarks?
Send me an email at [email protected], or die in a hole! Whichever comes first!