Coder Social home page Coder Social logo

aykutkardas / json-function Goto Github PK

View Code? Open in Web Editor NEW
751.0 24.0 34.0 607 KB

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

Home Page: https://worn.gitbook.io/json-function/

License: MIT License

TypeScript 100.00%

json-function's Introduction

Json Function

npm License Build Status

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

json-function's People

Contributors

absolux avatar aykutkardas avatar dependabot[bot] avatar fahimfaisaal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

json-function's Issues

Any plans to provide OR condition is this possible JsonFunction.where with deep:true?

Any plans to provide OR condition in JsonFunction.where((wh) => ...) with deep:true?

const data = [ { id: 1, metaData: { field1: "bobby", field2: 2 } }];

is this possible JsonFunction.where((wh) => ( [ {'metaData.field1': 'aa'}, {'metaData.field2': 2} ], {deep:true} ));

Note: why do we need to pass {deep: true} in where, orderBy etc? Can't we find it based on the key value, like if it has dot (e.g. metaData.field1) then enable deep automatically?

[PERF] too many loops inside where

Hi,

I've noticed a performance problem for where utility. There are many loops :

  • a loop to add UNIQUE_IDX_KEY for each query object
    let temp = data.map((item, index) => {
    if (!item[UNIQUE_IDX_KEY]) {
    item[UNIQUE_IDX_KEY] = index
    }
    return item;
    });
  • a loop to filter data
    temp = temp.filter((item) => {
    let value = item[fieldName];
    const activeQuery = query[fieldName];
    if (options && options.deep) {
    value = getObjDeepProp(fieldName)(item);
    }
    if (isFunction(activeQuery)) {
    return activeQuery(value);
    }
    return value === activeQuery;
    });
  • a loop to contruct the result array
    temp.forEach((tempItem) => {
    if (!isAlreadyDefine(result, tempItem)) {
    result.push(tempItem);
    }
    });
  • a loop to remove UNIQUE_IDX_KEY from the result array
    return result.map((item) => {
    delete item[UNIQUE_IDX_KEY];
    return item;
    });

IMO, you only need 1 loop to filter the data and return the result

Select & Schema

Hi,

I've read the source code, and have noticed that select is a simpler form of schema, where :

'fieldname' => { "fieldname": "fieldname" }

['field1', 'field2', ...] => { "field1": "field1", "field2": "field2", ... }

IMO, it needs some refactoring !!

Module not found: Error: Can't resolve 'json-function'

When using the latest version (1.8.16) getting Module not found: Error: Can't resolve 'json-function' issues.
import JsonFunction from 'json-function';
import { where } from 'json-function';
Tries to import in both ways but no luck. But works in version 1.8.14. Anything i am missing here.

Thank You!

Is there any possibility to do deep orderby?

Is there any possibility to do deep orderby?
const data = [{ id: 3, userId: 2, title: "delectus aut autem", metadata: {field1: 'bb', field2: 'btest'} }, { id: 2, userId: 1, title: "lorem ipsum", metadata: {field1: 'aa', field2: 'atest'} }];
orderBy(data, "metadata.field1", "ASC");

Its a awesome library, which reduces most of my time and code. 👍
Thank you in advance!

[PERF] processes priority order

Hi,

In JsonFunction class, the commands are executed in the definition order, not order of priority.

For example in this code, the order is select -> where -> orderBy -> limit

JsonFunction
  .select(["title", "completed"])
  .where({ owner: "John" })
  .orderBy("title")
  .limit(2)

but the priority order will be where -> orderBy -> limit -> select

select and schema are the lesser priority command, in case where returns nothing, select should process nothing too.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.