JSPM

  • Created
  • Published
  • Downloads 38
  • Score
    100M100P100Q75409F

Package Exports

  • map-fns
  • map-fns/index.esm.js
  • map-fns/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (map-fns) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

map-fns

Easily manipulate key-value stores in the browser and Node.js.


Why map-fns?

  • Zero dependencies: Keep your deployments and node modules lightweight.
  • Modular: Import the functions you need, tree-shake the rest.
  • Immutable & Pure: Function arguments are not modified and new objects are returned.
  • TypeScript: Well documented and fully typed.
  • Plain JavaScript objects: Keep things simple. No custom classes.
  • Open Source: MIT licensed.
import { mergeInMap } from "map-fns";

const map = {
  alice: {
    name: "Alice",
    permissions: ["view"],
  },
  bob: {
    name: "Bob",
    permissions: ["view", "merge", "push"],
  },
};

mergeInMap(map, "alice", {
  permissions: (permissions) => [...permissions, "merge"],
});
//=> {
//     alice: {
//       name: "Alice",
//       permissions: ["view", "merge"],
//     },
//     bob: {
//       name: "Bob",
//       permissions: ["view", "merge", "push"],
//     },
//   }

map-fns supports tree-shaking. If your environment does not you can import a specific function directly.

import mergeInMap from "map-fns/mergeInMap";

Functions

map-fns exports a variety of functions that can be used to easily manipulate key-value stores.

Examples have yet to be created for functions that are not a link.

addListToMap

Examples of using addListToMap.

Use addListToMap to add a list of entries to a map.

import { addListToMap } from "map-fns";

const map = {
  a: { id: "a", value: 1 },
  b: { id: "b", value: 2 },
  c: { id: "c", value: 3 },
};

addListToMap(
  map,
  [
    { id: "d", value: 4 },
    { id: "e", value: 5 },
  ],
  "id"
);

//=> {
//     a: { id: "a", value: 1 },
//     b: { id: "b", value: 2 },
//     c: { id: "c", value: 3 },
//     d: { id: "d", value: 4 },
//     d: { id: "e", value: 5 },
//   }

addListToMap assumes that there is a key field (such as id in this example) whose value is equal to the entry's key in the map.

mapEntries

Examples of using mapEntries.

Use mapEntries to get the list of key-value entries in a map.

import { mapEntries } from "map-fns";

const map = { a: 1, b: 2, c: 3 };

mapEntries(map);
//=> [
//     ["a", 1],
//     ["b", 2],
//     ["c", 3]
//   ]

A list of keys can be provided as the second argument to only return the entries for those keys.

import { mapEntries } from "map-fns";

const map = { a: 1, b: 2, c: 3 };

// The order of the keys determines the order of the entries

mapEntries(map, ["c", "b"]);
//=> [
//     ["c", 3]
//     ["b", 2],
//   ]

If a provided key does not exist in the map an error is thrown.

mapMap

Examples of using mapMap.

Use mapMap to transform every value in map to a new value with a callback function.

import { mapMap } from "map-fns";

const map = {
  a: 1,
  b: 2,
  c: 3,
};

mapMap(map, (n) => n * 2);
//=> {
//     a: 2,
//     b: 4,
//     c: 6,
//   }

mergeInMap

Examples of using mergeInMap.

Use mergeInMap to modify entries in a map deeply.

import { mergeInMap } from "map-fns";

const companies = {
  apple: {
    name: "Apple Inc.",
    headquarters: {
      country: "United States",
      address: "1 Apple Park Way",
    },
  },
  google: {
    name: "Google LLC",
    headquarters: {
      country: "United States",
      address: "1600 Amphitheatre Parkway",
    },
  },
};

// Move Google's headquarters

mergeInMap(companies, "google", {
  headquarters: {
    address: "50 Quantum Avenue St.",
  },
});
//=> {
//     apple: {
//       name: "Apple Inc.",
//       headquarters: {
//         country: "United States",
//         address: "1 Apple Park Way",
//       },
//     },
//     google: {
//       name: "Google LLC",
//       headquarters: {
//         country: "United States",
//         address: "1600 Amphitheatre Parkway",
//       },
//     },
//   }

Instead of providing a property directly, a callback can be provided:

import { mergeInMap } from "map-fns";

const departments = {
  engineering: {
    name: "Engineering",
    employees: ["Alex", "Brandon", "Caitlin"],
  },
  design: {
    name: "Design",
    employees: ["Daniela", "Evan"],
  },
};

// Let's welcome Francesca to the Design team

mergeInMap(departments, "design", {
  employees: (employees) => [...employees, "Francesca"],
});
//=> {
//     engineering: {
//       name: "Engineering",
//       employees: ["Alex", "Brandon", "Caitlin"],
//     },
//     design: {
//       name: "Design",
//       employees: ["Daniela", "Evan", "Francesca"],
//     },
//   }

By providing an array of keys, multiple entries in the map can be modified at once:

import { mergeInMap } from "map-fns";

const employees = {
  alice: {
    name: "Alice Thompson",
    salary: 160_000,
  },
  bob: {
    name: "Bob Smith",
    salary: 120_000,
  },
  charlie: {
    name: "Charlie Miller",
    salary: 145_000,
  },
};

// Give Alice and Bob a 10% raise

mergeInMap(employees, ["alice", "bob"], {
  salary: (salary) => salary * 1.1,
});
//=> {
//     alice: {
//       name: "Alice Thompson",
//       salary: 176_000,
//     },
//     bob: {
//       name: "Bob Smith",
//       salary: 132_000,
//     },
//     charlie: {
//       name: "Charlie Miller",
//       salary: 145_000,
//     },
//   }

partialMap

Examples of using partialMap.

Use partialMap to get a copy of a map only including the keys provided in the second argument.

import { partialMap } from "map-fns";

const map = {
  a: 1,
  b: 2,
  c: 3,
};

partialMap(map, ["a", "b"]);
//=> {
//     a: 1,
//     b: 2,
//   }

removeKeysFromMap

Examples of using removeKeysFromMap.

Use removeKeysFromMap to get a copy of a map excluding the keys provided in the second argument.

import { removeKeysFromMap } from "map-fns";

const map = {
  a: 1,
  b: 2,
  c: 3,
};

removeKeysFromMap(map, ["a", "c"]);
//=> {
//     b: 2,
//   }

If you only need to remove a single key from the map, that key may be provided directly as the second argument.

import { removeKeysFromMap } from "map-fns";

const map = {
  a: 1,
  b: 2,
  c: 3,
};

removeKeysFromMap(map, "c");
//=> {
//     a: 1,
//     b: 2,
//   }