json-comb-core examples

Table of Contents

Quick Take

import { strict as assert } from "assert";
import {
  getKeysetSync,
  getKeyset,
  enforceKeyset,
  enforceKeysetSync,
  sortAllObjectsSync,
  noNewKeysSync,
  findUnusedSync,
} from "json-comb-core";

// STEP #1
// =======

// calculate the schema - superset of all possible keys used across
// all JSON files
const schema = getKeysetSync([
  {
    // <- object #1
    a: "a",
    b: "c",
    c: {
      d: "d",
      e: "e",
    },
  },
  {
    // <- object #2
    a: "a",
  },
  {
    // <- object #3
    c: {
      f: "f",
    },
  },
]);

assert.deepEqual(schema, {
  a: false,
  b: false,
  c: {
    d: false,
    e: false,
    f: false,
  },
});

// STEP #2
// =======

// now we can normalise the object #2 for example:
assert.deepEqual(
  enforceKeysetSync(
    {
      // <- object #2
      a: "a",
    },
    schema
  ),
  {
    a: "a",
    b: false,
    c: {
      d: false,
      e: false,
      f: false,
    },
  }
);

Asynchronous enforceKeyset()

import { strict as assert } from "assert";
import {
  getKeysetSync,
  getKeyset,
  enforceKeyset,
  enforceKeysetSync,
  sortAllObjectsSync,
  noNewKeysSync,
  findUnusedSync,
} from "json-comb-core";

// Let's enforce the keyset using previously-calculated schema.

// Let's use an async IIFE to trigger the action and let us use await:
(async () => {
  // let's create three plain objects, each somewhat overlapping with others:
  const obj1 = {
    b: [
      {
        c: "ccc",
        d: "ddd",
      },
    ],
    a: "aaa",
  };
  const obj2 = {
    a: "ccc",
    e: "eee",
  };
  const obj3 = {
    a: "zzz",
  };
  // calculate the schema:
  const schema = await getKeyset([obj1, obj2, obj3]);

  assert.deepEqual(schema, {
    a: false,
    b: [
      {
        c: false,
        d: false,
      },
    ],
    e: false,
  });

  assert.deepEqual(await enforceKeyset(obj1, schema), {
    a: "aaa",
    b: [
      {
        c: "ccc",
        d: "ddd",
      },
    ],
    e: false, // <------ new key added
  });

  assert.deepEqual(await enforceKeyset(obj2, schema), {
    a: "ccc",
    b: [
      // <------- new key added
      {
        c: false,
        d: false,
      },
    ],
    e: "eee",
  });

  assert.deepEqual(await enforceKeyset(obj3, schema), {
    a: "zzz",
    b: [
      // <------- new key added
      {
        c: false,
        d: false,
      },
    ],
    e: false, // <------- new key added
  });
})();

Quick Take

import { strict as assert } from "assert";
import {
  getKeysetSync,
  getKeyset,
  enforceKeyset,
  enforceKeysetSync,
  sortAllObjectsSync,
  noNewKeysSync,
  findUnusedSync,
} from "json-comb-core";

const schema = {
  a: false,
  b: false,
  c: {
    d: false,
    e: false,
    f: false,
  },
};

assert.deepEqual(
  enforceKeysetSync(
    {
      c: { d: "x" },
    },
    schema
  ),
  {
    a: false,
    b: false,
    c: {
      d: "x",
      e: false,
      f: false,
    },
  }
);

Synchronous findUnusedSync() - example #2

import { strict as assert } from "assert";
import {
  getKeysetSync,
  getKeyset,
  enforceKeyset,
  enforceKeysetSync,
  sortAllObjectsSync,
  noNewKeysSync,
  findUnusedSync,
} from "json-comb-core";

// This function will work on arrays of both normalised and not normalised object sets.
assert.deepEqual(
  findUnusedSync([
    {
      a: [
        {
          k: false,
          l: false,
          m: false,
        },
        {
          k: "k",
          l: false,
          m: "m",
        },
      ],
      b: "bbb1",
      c: false,
    },
    {
      a: [
        {
          k: "k",
          l: false,
          m: "m",
        },
        {
          k: "k",
          l: false,
          m: "m",
        },
      ],
      b: "bbb2",
      c: false,
    },
    { b: false },
    { c: false },
  ]),
  ["c", "a[0].l"]
);

Synchronous findUnusedSync() - example #1

import { strict as assert } from "assert";
import {
  getKeysetSync,
  getKeyset,
  enforceKeyset,
  enforceKeysetSync,
  sortAllObjectsSync,
  noNewKeysSync,
  findUnusedSync,
} from "json-comb-core";

assert.deepEqual(
  findUnusedSync([
    {
      // <- object #1
      a: false,
      b: "bbb1",
      c: false,
    },
    {
      // <- object #2
      a: "aaa",
      b: "bbb2",
      c: false,
    },
    {}, // <- object #3
  ]),
  ["c"]
);

Asynchronous getKeyset()

import { strict as assert } from "assert";
import {
  getKeysetSync,
  getKeyset,
  enforceKeyset,
  enforceKeysetSync,
  sortAllObjectsSync,
  noNewKeysSync,
  findUnusedSync,
} from "json-comb-core";

// Let's calculate the schema of the following arrays of plain objects,
// and do it asynchronously.

// Let's use an async IIFE to trigger the action and let us use await:
(async () => {
  // First, prepare array of promises:
  const source = [
    {
      a: "a",
      b: "c",
      c: {
        d: "d",
        e: "e",
      },
    },
    {
      a: "a",
    },
    {
      c: {
        f: "f",
      },
    },
  ].map((el) => Promise.resolve(el));

  // use async/await to avoid using .then
  assert.deepEqual(await getKeyset(source), {
    a: false,
    b: false,
    c: {
      d: false,
      e: false,
      f: false,
    },
  });
})();

Synchronous getKeysetSync()

import { strict as assert } from "assert";
import {
  getKeysetSync,
  getKeyset,
  enforceKeyset,
  enforceKeysetSync,
  sortAllObjectsSync,
  noNewKeysSync,
  findUnusedSync,
} from "json-comb-core";

const schema = getKeysetSync([
  {
    // <- object #1
    a: "a",
    b: "c",
    c: {
      d: "d",
      e: "e",
    },
  },
  {
    // <- object #2
    a: "a",
  },
  {
    // <- object #3
    c: {
      f: "f",
    },
  },
]);

assert.deepEqual(schema, {
  a: false,
  b: false,
  c: {
    d: false,
    e: false,
    f: false,
  },
});

Synchronous noNewKeysSync() - example #2

import { strict as assert } from "assert";
import {
  getKeysetSync,
  getKeyset,
  enforceKeyset,
  enforceKeysetSync,
  sortAllObjectsSync,
  noNewKeysSync,
  findUnusedSync,
} from "json-comb-core";

assert.deepEqual(
  noNewKeysSync(
    {
      // <- input we're checking
      z: [
        {
          a: "a",
          b: "b",
          c: "c",
        },
        {
          a: false,
          b: false,
          c: "c",
        },
      ],
    },
    {
      // <- reference keyset
      z: [
        {
          a: "a",
          b: "b",
        },
        {
          a: false,
          b: false,
        },
      ],
    }
  ),
  ["z[0].c", "z[1].c"]
);

Synchronous noNewKeysSync() - example #1

import { strict as assert } from "assert";
import {
  getKeysetSync,
  getKeyset,
  enforceKeyset,
  enforceKeysetSync,
  sortAllObjectsSync,
  noNewKeysSync,
  findUnusedSync,
} from "json-comb-core";

// We are going to catch the rogue key `b`:

assert.deepEqual(
  noNewKeysSync(
    {
      // <- input we're checking
      a: "a",
      b: "b",
      c: "c",
    },
    {
      // <- reference keyset
      a: "aaa",
      c: "ccc",
    }
  ),
  ["b"] // list of rogue paths
);

Synchronous getKeysetSync()

import { strict as assert } from "assert";
import {
  getKeysetSync,
  getKeyset,
  enforceKeyset,
  enforceKeysetSync,
  sortAllObjectsSync,
  noNewKeysSync,
  findUnusedSync,
} from "json-comb-core";

assert.deepEqual(
  sortAllObjectsSync({
    a: "a",
    c: "c",
    b: "b",
  }),
  {
    a: "a",
    b: "b",
    c: "c",
  }
);