JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 148
  • Score
    100M100P100Q67387F
  • License GPL-3.0

Query JSON with one line of code.

Package Exports

  • 1liner

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (1liner) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

1Liner

build status version downloads

1Liner, or simply L is a super fast (10000 query executions take 150ms) and lightweight (< 35 kb minified) JavaScript library for the browser or for Node.js that allows querying of JSON with one tiny line of code.

Why?

Fetching data from a JSON can be cumbersome and always requires too much code. Wouldn't it be great to be able to fetch data, quickly, from a JSON with one simple line?

const L = require('1liner');
const obj = new L({
    propose: {
        address: {
            line_1: "39944 Morissette Trail",
            line_2: "Gulgowski Wells",
            postcode: "AB13RT",
            county: "Gloucestershire",
            country: "GB"
        },
        convictions: [{
            code: "SP50",
            points: 4,
        },
        {
            code: "SP50",
            points: 2,
        },
        {
            code: "SP30",
            points: 1,
        }]
    },
    additional_drivers: [{
        name: 'Ted',
        age: 24
    }, {
        name: 'Mary',
        age: 30
    }]
});

/*
* Normal object syntax
*/ 
obj.query('proposer.address.postcode'); // "AB13RT" 

/*
* Normal object then map on array element
*/ 
obj.query('proposer.convictions.map(code)'); // ["SP50", "SP50", "SP30"]

/*
* Normal object then chain array element filter on filter
*/ 
obj.query('proposer.convictions.filter(points<=2).filter(code=SP30).map(code)'); // ["SP30"]
obj.query('proposer.convictions.filter(points<=2).filter(code="SP30").map(code)'); // ["SP30"]

/*
* Array first element so filter applied immediately
*/ 
obj.query('additional_drivers.filter(age>24).map(name)'); // ["Mary"]

Installation

npm install --save 1liner

Usage

Node.js

const L = require('1liner');

const obj = new L({
    proposer: {
        title: "MR",
        first_names: "Toadie",
        last_names: "Ezakeeper",
        age: 33,
        claims: [{
            code: "A",
            at_fault: false,
        }]
    }    
});

obj.query('proposer.title'); // "MR"
obj.query('proposer.claims.map(code)'); // ["A"]
obj.query('proposer.age') - 10; // 23

Browsers

<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/caljrimmer/1liner@latest/dist/index.js"></script>

A global variable window.L or simply L is created.

Operators

See test file for more details.

- count

Counts the number of items in an array. It can be used after a filter has been applied.

Example:

const L = require('1liner');

const obj = new L({
    proposer: {
        title: "MR",
        first_names: "Toadie",
        last_names: "Ezakeeper",
        age: 33,
        claims: [{
            code: "A",
            at_fault: false,
        },
        {
            code: "W",
            at_fault: false,
        }]
    }    
});

obj.query('proposer.claims.count()'); // 2
obj.query('proposer.claims.filter(code=W).count()'); // 1
obj.query('proposer.claims.filter(code="W").count()'); // 1

- min, max, mean, range

Finding the min integer, max integer and range between the two.

Example:

const L = require('1liner');

const obj = new L({
    proposer: {
        title: "MR",
        first_names: "Toadie",
        last_names: "Ezakeeper",
        age: 33,
        convictions: [{
            code: "SP50",
            points: 4
        },
        {
            code: "SP30",
            points: 2
        }]
    }    
});

obj.query('proposer.convictions.map(points).min()'); // 2
obj.query('proposer.convictions.map(points).max()'); // 4
obj.query('proposer.convictions.map(points).range()'); // 2
obj.query('proposer.convictions.map(points).mean()'); // 3

/**
 * Defaulting result on min, max, mean
 */
 
obj.query('proposer.convictions.filter(points=0).min(10)'); // 10
obj.query('proposer.convictions.filter(points=0).max(1)'); // 1
obj.query('proposer.convictions.filter(points=0).mean(3)'); // 3

- map

Returns an array of the selected key within a collection of items.

Example:

const L = require('1liner');

const obj = new L({
    proposer: {
        title: "MR",
        first_names: "Toadie",
        last_names: "Ezakeeper",
        age: 33,
        convictions: [{
            code: "SP50",
            points: 4
        },
        {
            code: "SP30",
            points: 2
        }]
    }    
});

obj.query('proposer.convictions.map(code)'); // ["SP50", "SP30"]

- filter

Returns a filtered collection based on a criteria.

Filters work on strings and numbers. Dates will need to be converted in to numbers (UNIX timestamps) to be filtered.

Example:

const L = require('1liner');

const obj = new L({
    proposer: {
        title: "MR",
        first_names: "Toadie",
        last_names: "Ezakeeper",
        age: 33,
        convictions: [{
            code: "SP50",
            points: 4
        },
        {
            code: "SP30",
            points: 2
        }],
        claims: [{
            code: "A",
            at_fault: false,
        },
        {
            code: "W",
            at_fault: true,
        }]
    }    
});

obj.query('proposer.convictions.filter(code=SP30)'); // [{ code: "SP30", points: 2 }]
obj.query('proposer.convictions.filter(code="SP30")'); // [{ code: "SP30", points: 2 }]
obj.query('proposer.convictions.filter(code=SP30).map(points)'); // [2]
obj.query('proposer.convictions.filter(code!=SP30).map(points)'); // [4]
obj.query('proposer.convictions.filter(points<4).map(code)'); // ["SP30"]
obj.query('proposer.convictions.filter(points<=4).map(code)'); // ["SP50", "SP30"]
obj.query('proposer.convictions.filter(points>2).map(code)'); // ["SP50"]
obj.query('proposer.convictions.filter(points>=2).map(code)'); // ["SP50", "SP30"]
obj.query('proposer.claims.filter(at_fault=true).map(code)'); // ["W"]
obj.query('proposer.claims.filter(at_fault=false).count()'); // 1

- unique

Returns an array of the unique items.

Example:

const L = require('1liner');

const obj = new L({
    proposer: {
        title: "MR",
        first_names: "Toadie",
        last_names: "Ezakeeper",
        age: 33,
        convictions: [{
            code: "SP50",
            points: 4
        },
        {
            code: "SP50",
            points: 2
        }]
    }    
});

obj.query('proposer.convictions.unique(code).count()'); // 1

- exists

Checks if property value exists i.e. not null, undefined, empty string or 0.

Example:

const L = require('1liner');

const obj = new L({
    proposer: {
        title: "MR",
        first_names: "Toadie",
        middle_name: "",
        last_names: "Ezakeeper",
        age: 0,
        spouse: null
    }    
});

obj.query('proposer.middle_name.exists()'); // "false"
obj.query('proposer.age.exists()'); // "false"
obj.query('proposer.spouse.exists()'); // "false"
obj.query('proposer.title.exists()'); // "true"

Testing

Node.js

Install the dev dependencies:

$ npm install 1liner --development

Then navigate to the installed directory:

$ cd node_modules/1liner/

Run test package:

$ npm test

Speed

  • Small Object (2.5KB) - 10000 query executions takes 150ms
  • Large Object (2.5MB) - 1000 query executions takes 1094ms

Bundling

npm run bundle

Contributions

If you contribute to this library, just modify index.js, index.spec.js, and update README.md. I'll update the website docs and generate the new dist/index.js, changelog and version.

Alternative packages

The following packages allow more sophisticated querying of JSON.

License

Licensed under GNU GENERAL PUBLIC LICENSE.

Copyright (C) 2020 Callum Rimmer callum@deadtrendy.co.uk