aykutkardas/json-function
Über uns
JSON-Objektarrays lassen sich mit where, search, select, orderBy, limit, schema und joins abfragen. Die Bibliothek ist typisiert, kommt ohne Abhängigkeiten aus und arbeitet auch mit großen Arrays schnell.
EntwicklerwerkzeugeGemessener Sternzuwachs
- in 24h
- 0
- in 7 days
- 0
- in 15 days
- Verlauf wird gesammelt
- in 1 month
- Verlauf wird gesammelt
Nettoänderungen in überlappenden Zeiträumen, keine Tagessummen. Fehlende Messwerte bleiben leer. Sterne zeigen Interesse, nicht Qualität.
README.md
OriginalJson Function
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.
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
resetRecordoption and theoption/dataproperties 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 bysetQuery()andget(data, { query }).innerJoinis a real inner join. Items without a match are dropped and an item with several matches appears once per match. UseleftJointo keep unmatched items.wherewith several queries returns each matching item once, in input order.orderByno longer sorts the input array in place.searchmatches the key literally (no regular expressions), skips missing fields, and is case sensitive unlesscaseSensitive: falseis passed.transformand.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 ascamelcase-keys(camelcaseKeys(data, { deep: true })), or rename specific fields withschema(data, { userId: "user_id" }).ES module only, Node.js 20.19+. The package no longer ships a separate CommonJS build.
importworks everywhere, andrequire("json-function")keeps working on Node.js 20.19+ and 22.12+, which can load ES modules withrequire. On older Node.js versionsrequirefails withERR_REQUIRE_ESM.TypeScript: functions are generic, so results keep their item type.
Ähnliche Projekte entdecken
Wird geladen…