JSPM

  • Created
  • Published
  • Downloads 185
  • Score
    100M100P100Q64653F
  • License Apache-2.0

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

DocumentationChangelog

Install

npm install json-function

Usage

JsonFunction • documentation

Chaining

import JsonFunction from "json-function";

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

or Basic

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);

or create a query and use it at any time.

const queryTwoIncompleteTasks = JsonFunction
  .where({ completed: false })
  .select(["title", "completed"])
  .limit(2)
  .getQuery();
  

Query usage

JsonFunction.setQuery(queryTwoIncompleteTasks).get(data);
// or
JsonFunction.get(data, { query: queryTwoIncompleteTasks });

Methods

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

innerJoin • documentation

import { innerJoin } from "json-function";

innerJoin(data, data2, "id", "userId");

schema • documentation

import { schema } from "json-function";

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

Use "callback" for advanced conversions.

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

Custom seperator

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

Use your own special function.

schema(data, (sc) => ({
  id: 'id',
  fullName: sc.custom(
    (firstname, lastname) => `${firstname.toUpperCase()} ${lastname.toUpperCase()}`,
    "user.firstname",
    "user.lastname"
  ),
}))

Example

schema(data, (sc) => ({
  id: 'id',
  createdAt: sc.custom(
    (createdAt) => moment(createdAt).format('DD/MM/YYYY'),
    "createdAt",
  ),
}))

where • documentation

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 • documentation

import { select } from "json-function";

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

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

limit • documentation

import { limit } from "json-function";

// limit
limit(data, 2);

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

orderBy • documentation

import { orderBy } from "json-function";

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