1.3.1 • Published 6 months ago

jsoq v1.3.1

Weekly downloads
6
License
ISC
Repository
github
Last release
6 months ago

jsoq

Query and manipulate JSON arrays easily. Powered by lodash, inspired by knex syntax and SQL in general.

Installation

npm i jsoq@latest

Usage

/*** 1. Import jsoq package: ***/  
import jsoq from 'jsoq';
// or const jsoq = require('jsoq');

const data = [
  { a: 1, b: 'some text', c: false },
  { a: 2, b: 'another text', c: true },
];

/*** 2. Set the JSON array you want to query: ***/
const output = jsoq.from(data)
  /*** 3. Start manipulating: ***/
  .where({ c: true })
  .select('a')
  /*** 4. Manipulation done, return result: ***/
  .toJSON();
//-> [ { a: 2 } ]

Functions

Basic

  • .from(json: object[]) - Sets the input JSON array.
    jsoq.from(data); //-> jsoq
  • .toJSON(flatten?: boolean) - Returns current state of input JSON array.
    jsoq.from(data).toJSON(); //-> [{}, {}, ...]
    jsoq.from(data).first().toJSON(true); //-> {}
  • .toString() - Returns current state of input JSON array as string.
    jsoq.from(data).toString(); //-> "[{}, {}, ...]"

Aggregation

  • .avg(property: string) - Computes the average value of a property in JSON array.
    jsoq.from(data).avg('index'); //-> 3.5
  • .count() - Computes the number of objects in JSON array.
    jsoq.from(data).count(); //-> 6
  • .max(property: string, whole?: boolean) - Finds the maximum value of a property in JSON array.
    jsoq.from(data).max('age'); //-> 40
    jsoq.from(data).max('age', true); //-> jsoq
  • .min(property: string, whole?: boolean) - Finds the minimum value of a property in JSON array.
    jsoq.from(data).min('age'); //-> 21
    jsoq.from(data).min('age', true); //-> jsoq
  • .sum(property: string) - Computes the summation of a property in JSON array.
    jsoq.from(data).sum('age'); //-> 68

Filtering

  • .between(property: string, range: [min: any, max: any]) - Takes only the objects which are greater than or equals to the first value in range and smaller than or equals to the the second.
    jsoq.from(data).between('age', [10, 20]); //-> jsoq
    jsoq.from(data).between('name', ['A', 'B']); //-> jsoq
  • .distinct(property?: string) - Keeps only the first occurrence of a property in each object in JSON array.
    jsoq.from(data).distinct(); //-> jsoq
    jsoq.from(data).distinct('age'); //-> jsoq
  • .first(n?: number) - Takes n objects from the beginning of JSON array.
    jsoq.from(data).first(); //-> jsoq
    jsoq.from(data).first(3); //-> jsoq
  • .ilike(property: string, values: string | string[]) - Takes only the objects which match at list one pattern (case insensitive) in JSON array.
    jsoq.from(data).ilike('name', 'sha%'); //-> jsoq
    jsoq.from(data).ilike('name', ['sha%', '%ro%']); //-> jsoq
  • .in(property: string, values: any[]) - Takes only the objects which property value exists in given array.
    jsoq.from(data).in('index', [1, 2]); //-> jsoq
  • .last(n?: number) - Takes n objects from the end of JSON array.
    jsoq.from(data).last(); //-> jsoq
    jsoq.from(data).last(2); //-> jsoq
  • .like(property: string, values: string | string[]) - Takes only the objects which match at list one pattern (case sensitive) in JSON array.
    jsoq.from(data).like('name', 'Sha%'); //-> jsoq
    jsoq.from(data).like('name', ['Sha%', '%ro%']); //-> jsoq
  • .nth(n: number) - Takes the nth object from JSON array.
    jsoq.from(data).nth(4); //-> jsoq
  • .skip(n: number) - Takes all objects from JSON array, except of the first n objects.
    jsoq.from(data).skip(2); //-> jsoq
  • .where(predicate: any) - Takes only the objects which match the predicate in JSON array.
    jsoq.from(data).where({ age: 32 }); //-> jsoq
    jsoq.from(data).where('isActive'); //-> jsoq
    jsoq.from(data).where(o => o.age === 32 || o.isActive); //-> jsoq

Manipulation

  • .group(property: string) - Transforms JSON array into a dictionary, which composed of keys generated from the array.
    jsoq.from(data).group('age'); //-> { '21': [{}, {}...], '32': [{}...] }
  • .join(json: any[], property: string) - Merges two JSON arrays based on a property. Takes only objects which exist in both JSON arrays.
    jsoq.from(data).join(data2, 'index'); //-> jsoq
  • .leftJoin(json: any[], property: string) - Merges two JSON arrays based on a property. Takes all objects from left (first) JSON array.
    jsoq.from(data).leftJoin(data2, 'index'); //-> jsoq
  • .order(property: string) - Changes the order of all properties in array.
    jsoq.from(data).order('index'); //-> jsoq
    jsoq.from(data).order('index asc', 'age', 'isActive desc'); //-> jsoq
  • .rightJoin(json: any[], property: string) - Merges two JSON arrays based on a property. Takes all objects from the right (second) JSON array.
    jsoq.from(data).rightJoin(data2, 'index'); //-> jsoq
  • .select(property: string) - Extracts specific properties from all objects in array, with an option to rename keys.
    jsoq.from(data).select('index'); //-> jsoq
    jsoq.from(data).select('index as i', 'isActive as a'); //-> jsoq

Comments

  • All methods which return jsoq are chainable, hence .toJSON() should be called when you're ready to get the output.
  • first, last and nth functions return jsoq and not a single object, to enable chaining and for the sake of consistency.
  • Check tests directory for examples.
1.3.1

6 months ago

1.2.0

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.0.20

2 years ago

1.2.1

2 years ago

1.0.3

2 years ago

0.0.18

2 years ago

0.0.19

2 years ago

0.0.17

3 years ago

0.0.16

4 years ago

0.0.15

4 years ago

0.0.14

5 years ago

0.0.13

5 years ago

0.0.12

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago