• Stars
    star
    751
  • Rank 60,419 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 7 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

Json Function

npm License Build Status

DocumentationChangelog

Lets you use where, limit, select, orderBy, and more in JSON data.

Install

npm install json-function

or

yarn add json-function

Usage

JsonFunction • documentation

Json-Function provides a lot of useful functions especially for your json data. It contains the methods you need too much to eliminate unnecessary code repetition.

You can use the Json-Function methods separately, but it is possible to use them all together. You can also chain it.

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

The "innerJoin" function is used to join two arrays.

import { innerJoin } from "json-function";

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

schema • documentation

The "Schema" function is a great way to reconfigure your json data and make it your own.

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 separator

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

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

The "Where" function provides a comfortable method for filtering a json data.

import { where } from "json-function";

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

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

// Deep
// (address.city === "New York")
where(data, { "address.city": "New York" }, { deep: true });

Use "callback" for advanced filter.

// id <= 3
where(data, (wh) => ({
  id: wh.lte(3),
}));

Other wh methods.

wh.lte(3)             // value <= 3
wh.lt(3)              // value <  3
wh.gte(3)             // value >= 3
wh.gt(3)              // value >  3
wh.between(3,5)       // value >= 3 && value <= 5
wh.eq("3")            // value == 3
wh.ne("3")            // value != 3
wh.in("test")         // value.includes("test")
wh.nin("test")        // !value.includes("test")
wh.oneOf([1, 2, 3])  // [1, 2, 3].includes(value)

select • documentation

The "Select" function is a practical method where you only get the desired fields of a json data.

import { select } from "json-function";

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

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

limit • documentation

"Limit" is used to get a limited number of elements from a json data. Almost javascript works like slice() but it is much easier and clearer.

import { limit } from "json-function";

// limit
limit(data, 2);

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

orderBy • documentation

With the "orderBy" function you can reorder the data in your json array.

import { orderBy } from "json-function";

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

orderBy(data, "user.firstname", "DESC", { deep: true });

search • documentation

Search over fields of objects.

import { search } from "json-function";

// Syntax: search(data: Object[], key: any, fields: string | string[], options?);

// single field
search(data, "key", "description");

// multiple field
search(data, "key", ["user.firstName", "description"]);

// case sensitive
search(data, "key", "description", { caseSensitive: false });

toArray • documentation

Converts objects into meaningful sequences.

import { toArray } from "json-function";

// default key "uid"
toArray(data);

// custom key
toArray(data, { key: "_id_" });

transform • documentation

JSON converts the snake_case keys in your data to camelCase.

import { transform } from "json-function";

transform(data);

More Repositories

1

regexlearn.com

Learn RegEx step by step, from zero to advanced.
TypeScript
3,366
star
2

svgps.app

SVGPS converts your icons into a single JSON file that is ready to use in your frontend or mobile projects.
TypeScript
293
star
3

react-icomoon

It makes it very simple to use SVG icons in your React and React-Native projects.
TypeScript
220
star
4

svg-quick-editor

SVG Quick Editor is a free and open-source SVG editing tool. It offers features such as editing SVG colors, viewing or deleting their paths.
JavaScript
111
star
5

lookie

Store your data in localStorage with optional expiration time. Almost like a cookie.
TypeScript
82
star
6

drain-js

Makes smooth transitions between two numbers.
TypeScript
46
star
7

ymir-js

This toolkit is created to make it easier for you to develop games like chess, checkers, go, match 3 puzzle and more. It is still under development.
TypeScript
35
star
8

nginx-cli

It is a command line tool that will simplify Nginx operations.
JavaScript
32
star
9

mastertime

Timer management library.
JavaScript
31
star
10

javascript-sheets

JavaScript hakkında açıklayıcı Türkçe örnekler.
29
star
11

react-leblebi

Autocomplete Component for React
JavaScript
28
star
12

enwords

Analyze and Track English Words - Chrome Extension
JavaScript
27
star
13

vue-icomoon

With vue-icomoon you can easily use the icons you have selected or created in icomoon.
Vue
26
star
14

checkers.place

It is a 3D, minimal and online checkers game.
TypeScript
24
star
15

svgps

Svg Parser
TypeScript
17
star
16

jquery-to-javascript

Converts simple jQuery codes to javascript codes.
TypeScript
16
star
17

focus-wo-extension

FocusWo extension lists the words of the text in the active browser tab by frequency of use. It also allows you to search between words.
Vue
9
star
18

react-dropdown-wrapper

Convenient, simple, quick dropdown menu wrapper for accessibility.
JavaScript
9
star
19

LinkRunner

Creator Sitemap with PHP
PHP
7
star
20

leblebi.js

Cool and easy to autocomplete library. ⚡️
JavaScript
7
star
21

tardiness.js

Simple and easy encryption. Less than 1 kb.
JavaScript
6
star
22

react-colorful-avatar

JavaScript
6
star
23

svelte-icomoon

It makes it very simple to use SVG icons in your Svelte projects.
JavaScript
5
star
24

class-methods-binder

"class-methods-binder" is an easy and standart way to bind the method.
TypeScript
5
star
25

rn-colorful-avatar

Colorful Avatar for React Native
JavaScript
4
star
26

status-code-check

Status code check of URLs
Go
4
star
27

File-Explorer

Read Dir and Walk in Dir
JavaScript
4
star
28

Array-Interface-Helper

Array Interface for PHP
PHP
3
star
29

syscli

System Information View and Search
JavaScript
3
star
30

react-ifelse

Condition Components for React
JavaScript
3
star
31

awesome-muninn

Muninn common configs and utils
3
star
32

svgps-cli

This project is the CLI version of the svgps package.
TypeScript
2
star
33

preact-icomoon

It makes it very simple to use SVG icons in your Preact projects.
TypeScript
2
star
34

next-boilerplate

Next.js 9 with typescript, mobx, axios, sass(+autoprefixer), bootstrap and more...
JavaScript
2
star
35

compute-svg-data

It is a common helper for "*-icomoon" packages.
TypeScript
2
star
36

get-obj-deep-prop

Check and get object prop.
JavaScript
2
star
37

FEXCH4NGE

Candy Crush Coding...
JavaScript
1
star
38

json-function-query-builder

Query Builder UI for Json-Function
1
star
39

word-counter.js

Word Counter
JavaScript
1
star
40

html-vschema

html-vschema creates a simplified and manipulable schema of html.
TypeScript
1
star
41

Qirpi

Jquery Alternative Library
JavaScript
1
star
42

React-ZwIcon

ZwIcon for ReactJS
JavaScript
1
star
43

gulp-scss-dom

Use of SCSS features in HTML elements.
JavaScript
1
star
44

scss-dom

Use of SCSS features in HTML elements.
JavaScript
1
star
45

Country-Worn-Theme

Lightweight, simple and traditional. Brackets Theme
1
star
46

turkish-checkers

Turkish Checkers game made with ymir-js
JavaScript
1
star
47

international-checkers

International Checkers game made with ymir-js
JavaScript
1
star
48

typescript-starter-kit

Typescript starter kit with unit test.
TypeScript
1
star