Skip to content
项目

关于

使用 where、search、select、orderBy、limit、schema 和 joins 查询 JSON 对象数组。具备类型支持、无依赖,在大型数组上运行快速。

开发工具

这些数字如何统计

实测星标增长

24小时
0
7天
0
15天
正在收集历史
1个月
正在收集历史

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文

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.

探索相关项目

加载中…