Skip to content
/ just Public

A library of dependency-free JavaScript utilities that do just one thing.

License

Notifications You must be signed in to change notification settings

angus-c/just

Repository files navigation

Just

A library of zero-dependency npm modules that do just one thing. A guilt-free alternative to those bulkier utility libraries. Ideal for PWA development or whenever bytes are precious.

Build status

We welcome contributions. Please follow our contribution guidelines.

Try 🍦

A REPL for every utility (powered by RunKit)

Read 📚

ES and CJS modules available for every utility

All packages support ES module or Common JS syntax without requiring transpilation

// esm (node / bundler)
import clone from 'just-clone';

// esm (native browser code)
import clone from './node_modules/just-clone/index.mjs';

// cjs
const clone = require('just-clone');

TypeScript

We've now added TypeScript definitions and tests for every Just utility

Browser/Platform Support 💻

Most utilities still work with any platform that supports ES5, but these are the earliest versions guranteed to support every utility. For guidance, any platform that supports spread in array literals will work with Just.

Chrome Safari Firefox Edge Node Mobile Safari Android Chrome
46 8 16 12 6.0 8 46

The Modules 📦

Collections

source

🍦 Try it

npm install just-diff
yarn add just-diff

Return an object representing the difference between two other objects Pass converter to format as http://jsonpatch.com

import {diff} from 'just-diff';

const obj1 = {a: 4, b: 5};
const obj2 = {a: 3, b: 5};
const obj3 = {a: 4, c: 5};

diff(obj1, obj2);
[
  { "op": "replace", "path": ['a'], "value": 3 }
]

diff(obj2, obj3);
[
  { "op": "remove", "path": ['b'] },
  { "op": "replace", "path": ['a'], "value": 4 }
  { "op": "add", "path": ['c'], "value": 5 }
]

// using converter to generate jsPatch standard paths
import {diff, jsonPatchPathConverter} from 'just-diff'
diff(obj1, obj2, jsonPatchPathConverter);
[
  { "op": "replace", "path": '/a', "value": 3 }
]

diff(obj2, obj3, jsonPatchPathConverter);
[
  { "op": "remove", "path": '/b' },
  { "op": "replace", "path": '/a', "value": 4 }
  { "op": "add", "path": '/c', "value": 5 }
]

// arrays
const obj4 = {a: 4, b: [1, 2, 3]};
const obj5 = {a: 3, b: [1, 2, 4]};
const obj6 = {a: 3, b: [1, 2, 4, 5]};

diff(obj4, obj5);
[
  { "op": "replace", "path": ['a'], "value": 3 }
  { "op": "replace", "path": ['b', 2], "value": 4 }
]

diff(obj5, obj6);
[
  { "op": "add", "path": ['b', 3], "value": 5 }
]

// nested paths
const obj7 = {a: 4, b: {c: 3}};
const obj8 = {a: 4, b: {c: 4}};
const obj9 = {a: 5, b: {d: 4}};

diff(obj7, obj8);
[
  { "op": "replace", "path": ['b', 'c'], "value": 4 }
]

diff(obj8, obj9);
[
  { "op": "replace", "path": ['a'], "value": 5 }
  { "op": "remove", "path": ['b', 'c']}
  { "op": "add", "path": ['b', 'd'], "value": 4 }
]

source

🍦 Try it

npm install just-diff-apply
yarn add just-diff-apply

Apply a diff object to an object. Pass converter to apply a http://jsonpatch.com standard patch

  import {diffApply} from 'just-diff-apply';

  const obj1 = {a: 3, b: 5};
  diffApply(obj1,
    [
      { "op": "remove", "path": ['b'] },
      { "op": "replace", "path": ['a'], "value": 4 },
      { "op": "add", "path": ['c'], "value": 5 }
    ]
  );
  obj1; // {a: 4, c: 5}

  const obj2 = {a: 3, b: 5};
  diffApply(obj2,
    [
      { "op": "move", "from": ['a'], "path": ['c']},
    ]
  );
  obj2; // {b: 5, c: 3}

  // using converter to apply jsPatch standard paths
  // see http://jsonpatch.com
  import {diffApply, jsonPatchPathConverter} from 'just-diff-apply'
  const obj3 = {a: 3, b: 5};
  diffApply(obj3, [
    { "op": "remove", "path": '/b' },
    { "op": "replace", "path": '/a', "value": 4 }
    { "op": "add", "path": '/c', "value": 5 }
  ], jsonPatchPathConverter);
  obj3; // {a: 4, c: 5}

  // arrays (array key can be string or numeric)
  const obj4 = {a: 4, b: [1, 2, 3]};
  diffApply(obj4, [
    { "op": "replace", "path": ['a'], "value": 3 }
    { "op": "replace", "path": ['b', 2], "value": 4 }
    { "op": "add", "path": ['b', 3], "value": 9 }
  ]);
  obj4; // {a: 3, b: [1, 2, 4, 9]}

  // nested paths
  const obj5 = {a: 4, b: {c: 3}};
  diffApply(obj5, [
    { "op": "replace", "path": ['a'], "value": 5 }
    { "op": "remove", "path": ['b', 'c']}
    { "op": "add", "path": ['b', 'd'], "value": 4 }
  ]);
  obj5; // {a: 5, b: {d: 4}}

source

🍦 Try it

npm install just-compare
yarn add just-compare

Compare two collections

import compare from 'just-compare';

// primitives: value1 === value2
// functions: value1.toString == value2.toString
// arrays: if length, sequence and values of properties are identical
// objects: if length, names and values of properties are identical
compare([1, [2, 3]], [1, [2, 3]]); // true
compare([1, [2, 3], 4], [1, [2, 3]]); // false
compare({a: 2, b: 3}, {a: 2, b: 3}); // true
compare({a: 2, b: 3}, {b: 3, a: 2}); // true
compare({a: 2, b: 3, c: 4}, {a: 2, b: 3}); // false
compare({a: 2, b: 3}, {a: 2, b: 3, c: 4}); // false
compare([1, [2, {a: 4}], 4]