npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-function

v1.8.39

Published

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

Downloads

292

Readme

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