🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more

simple-map

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple-map

A super simple utility for mapping one object to another using key-value pairs or a function.

0.1.0
latest
70

Supply Chain Security

100

Vulnerability

90

Quality

76

Maintenance

100

License

Unpopular package

Quality

This package is not very popular.

Found 1 instance in 1 package

Version published
Weekly downloads
5
25%
Maintainers
1
Weekly downloads
 
Created

README

simple-map

simple-map is a super small utility function to map one object to another based on a key-value-pair map or a function.

Examples

mapping with key-value pairs

var map = require('simple-map');

var person = { 
	firstName: "John",
	lastName: "Doe"
};

var m = {
	"firstName": "first_name",
	"lastName": "last_name"
};

map(person, m); // { "first_name": "John", "last_name": "Doe" }

mapping with a function

function convertKey(k) {
	var m = { "x": "a", "y": "b", "z": "c" };
	return m[k];
}

map({"x": 1, "y": 2, "z": 3}, convertKey); // { "a": 1, "b": 2, "c": 3 }

FYI - repattern is a nice complement to simple-map

var map = require('simple-map');
var repattern = require('repattern');

var columnCase = repattern.make('cam$_');
map({'firstName': 'John', 'lastName': 'Doe'}, columnCase); // {'first_name': 'John', 'last_name': 'Doe'}	

only mapping certain keys

In some cases, its useful to map with a function, but restrict to only specific properties. You can pass an array of keys to include for this.

var person = {
    first_name: "John",
    last_name: "Doe",
    password: "a"
}

function convertKey(k) {
    return k.toUpperCase().replace("_", "");
}

map(person, convertKey, ["first_name", "last_name"]); // { "FIRSTNAME": "John", "LASTNAME": "Doe" }

map.make

map.make is super useful. It creates a curried function with an enclosed map and include list. You can use this with array#map to map objects within a collection.

var peopleRecords = [{"first_name": "John", "last_name": "Doe", "password", "a"}];
var personMapper = map.make(convertKey, ['first_name', 'last_name']);
var dtos = peopleRecords.map(personMapper);

install

npm install simple-map

use

var map = require('simple-map');

FAQs

Package last updated on 17 Apr 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts