Skip to content
프로젝트

소개

where, search, select, orderBy, limit, schema, joins를 사용해 JSON 객체 배열을 쿼리할 수 있습니다. 타입을 지원하고 의존성이 없으며, 큰 배열에서도 빠릅니다.

개발 도구

이 수치를 측정하는 방법

측정된 스타 증가

in 24h
0
in 7 days
0
in 15 days
기록 수집 중
in 1 month
기록 수집 중

서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.

README.md

Original

Json Function

npm License Test

Use where, select, orderBy, limit and more on arrays of JSON objects. Each function works on its own, or you can chain them into a query. No dependencies, TypeScript types included, and fast on large arrays.

Documentation • Changelog

Install

npm install json-function

or

pnpm add json-function

Requires Node.js 20.19 or later. The package is an ES module; require("json-function") also works on these versions.

Usage

Chain methods and run the query with get:

import JsonFunction from "json-function";

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

Or use only the functions you need:

import { where, orderBy } from "json-function";

const incomplete = where(data, { completed: false });
const sorted = orderBy(incomplete, "title", "DESC");

Every chain method returns a new query, so partial queries can be reused, and getQuery()/setQuery() save and restore them. See Chaining.

Functions

Function What it does
where Filter by field values, with AND, OR, deep fields and comparison helpers (wh.gt, wh.in, ...)
search Find items whose fields contain a text
select Keep only some fields, including nested ones
orderBy Sort by a field, ascending or descending
limit Take a number of items from a start index
schema Rename, nest, join and compute fields
innerJoin / leftJoin Join two arrays on a field
toArray Turn an object of objects into an array

TypeScript

Types are included, and standalone functions keep the item type of their input: where(users, ...) returns User[] and select(users, ["id", "name"]) returns Pick<User, "id" | "name">[]. See TypeScript.

Migrating from 1.x

  • Chaining is immutable. Every method returns a new query instead of changing a shared one. Calling methods one by one without using the return value no longer builds a query:

    // 1.x
    JsonFunction.where({ completed: false });
    JsonFunction.limit(2);
    JsonFunction.get(data);
    
    // 2.x
    const query = JsonFunction.where({ completed: false }).limit(2);
    query.get(data);
    

    The resetRecord option and the option/data properties are gone.

  • getQuery() returns an ordered list of steps and the steps run in that order. Queries saved in the old object format are still accepted by setQuery() and get(data, { query }).

  • innerJoin is a real inner join. Items without a match are dropped and an item with several matches appears once per match. Use leftJoin to keep unmatched items.

  • where with several queries returns each matching item once, in input order.

  • orderBy no longer sorts the input array in place.

  • search matches the key literally (no regular expressions), skips missing fields, and is case sensitive unless caseSensitive: false is passed.

  • transform and .transform() are removed. Converting key casing is outside what this library queries, and the function was the source of a prototype pollution issue. Use a package such as camelcase-keys (camelcaseKeys(data, { deep: true })), or rename specific fields with schema(data, { userId: "user_id" }).

  • ES module only, Node.js 20.19+. The package no longer ships a separate CommonJS build. import works everywhere, and require("json-function") keeps working on Node.js 20.19+ and 22.12+, which can load ES modules with require. On older Node.js versions require fails with ERR_REQUIRE_ESM.

  • TypeScript: functions are generic, so results keep their item type.

관련 프로젝트 살펴보기

불러오는 중…