JSPM

  • Created
  • Published
  • Downloads 185
  • Score
    100M100P100Q65219F
  • License ISC

It allows you to use methods such as where, limit, select, orderBy on JSON data.

Package Exports

  • json-function

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 (json-function) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Json Function

Build Status License

Full Size ~4 kb

Install

npm install json-function

Usage

Full class

Example data

const data = [
  {
    userId: 1,
    id: 1,
    title: "delectus aut autem",
    completed: false
  },
  {
    userId: 1,
    id: 2,
    title: "quis ut nam facilis et officia qui",
    completed: false
  },
  {
    userId: 1,
    id: 3,
    title: "fugiat veniam minus",
    completed: false
  },
  {
    userId: 1,
    id: 4,
    title: "et porro tempora",
    completed: true
  }
];

Chaining

import JsonFunction from "json-function";

const result = JsonFunction.where({ completed: false })
  .select(["title", "completed"])
  .orderBy("title", "DESC")
  .limit(2)
  .get(data);

or Standart

import JsonFunction from "json-function";

JsonFunction.where({ completed: false });
JsonFunction.select(["title", "completed"]);
JsonFunction.orderBy("title", "DESC");
JsonFunction.limit(2);
const result = JsonFunction.get(data);

Output

[
  {
    title: "quis ut nam facilis et officia qui",
    completed: false
  },
  {
    title: "fugiat veniam minus",
    completed: false
  }
];

Config

resetRecord

JsonFunction.limit(2).get(data);
console.log(JsonFunction.option.limit); // null
console.log(JsonFunction.data); // []
JsonFunction.limit(2).get(data, { resetRecord: false });
console.log(JsonFunction.option.limit); // [2, 0]
console.log(JsonFunction.data); // [{..}, {..}]

// Manual Reset Method
JsonFunction.reset();

Methods

Instead of an entire "class", you can use only the methods you need.

Where

Size ~0.505 kb

import { Where } from "json-function";

// Single
// (completed === false)
Where(data, { completed: false });

// Multiple (or)
// (completed === false || userId === 2)
Where(data, [{ completed: false }, { userId: 2 }]);

Select

Size ~0,402 kb

import { Select } from "json-function";

// Single
Select(data, "title");

// Multiple
Select(data, ["title", "completed"]);

Limit

Size ~0.273 kb

import { Limit } from "json-function";

// Limit
Limit(data, 2);

// Limit and Start
Limit(data, 2, 2);

OrderBy

Size ~0.488 kb

import { OrderBy } from "json-function";

OrderBy(data, "title", "DESC");

Schema

Size ~0.617 kb

Example data

const data = [
  {
    id: 0,
    user: {
      firstname: "John",
      lastname: "Doe"
    },
    title: "Book Name"
  },
  {
    id: 1,
    user: {
      firstname: "Johnny",
      lastname: "Doe"
    },
    title: "Book Name 2"
  }
];
import { Schema } from "json-function";

Schema(data, {
  book: {
    id: "id",
    title: "title"
  },
  firstname: "user.firstname",
  lastname: "user.lastname"
});

Output

[
  {
    firstname: "John",
    lastname: "Doe",
    book: {
      id: 0,
      title: "Book Name"
    }
  },
  {
    firstname: "Johnny",
    lastname: "Doe",
    book: {
      id: 1,
      title: "Book Name 2"
    }
  }
];

Schema Tool

Use "callback" for advanced conversions.

Schema(data, (sc) => ({
  id: 'id',
  fullName: sc.join('user.firstname', 'user.lastname')
}));

Output

[
  {
    id: 0,
    fullName: "John Doe"
  },
  {
    id: 1,
    fullName: "Johnny Doe"
  }
];

Custom seperator

Schema(data, (sc) => ({
  id: 'id',
  fullName: sc.join('user.firstname', 'user.lastname').with('_')
}));

Output

[
  {
    id: 0,
    fullName: "John_Doe"
  },
  {
    id: 1,
    fullName: "Johnny_Doe"
  }
];