1liner 中文文档教程

发布于 4年前 浏览 26 项目主页 更新于 3年前

1Liner

构建状态 版本 downloads

1Liner,或者简称为 L 是一个超快(10000 次查询执行需要 150 毫秒)和轻量级(<35 kb 缩小)的 JavaScript 库,用于允许使用一小行代码查询 JSON 的浏览器或 Node.js。

Why?

从 JSON 中获取数据可能很麻烦,而且总是需要太多代码。 能够通过一行简单的代码从 JSON 中快速获取数据不是很好吗?

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: "John",
        last_names: "Smith",
        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>

创建了一个全局变量 window.L 或简单的 L

Operators

有关详细信息,请参阅测试文件

- count

计算数组中的项目数。 它可以在应用过滤器后使用。

示例:

const L = require('1liner');

const obj = new L({
    proposer: {
        title: "MR",
        first_names: "John",
        last_names: "Smith",
        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, sum

查找最小整数、最大整数和两者之间的范围。

示例:

const L = require('1liner');

const obj = new L({
    proposer: {
        title: "MR",
        first_names: "John",
        last_names: "Smith",
        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
obj.query('proposer.convictions.sum(points)'); // 6

/**
 * 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

返回项目集合中选定键的数组。

示例:

const L = require('1liner');

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

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

- filter

返回基于条件过滤的集合。

过滤器适用于字符串和数字。 日期将需要转换为要过滤的数字(UNIX 时间戳)。

示例:

const L = require('1liner');

const obj = new L({
    proposer: {
        title: "MR",
        first_names: "John",
        last_names: "Smith",
        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

返回唯一项目的数组。

示例:

const L = require('1liner');

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

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

- exists

检查属性值是否存在,即不为空、未定义、空字符串或 0。

示例:

const L = require('1liner');

const obj = new L({
    proposer: {
        title: "MR",
        first_names: "John",
        middle_name: "",
        last_names: "Smith",
        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"

- default

如果属性未定义,则它将返回定义的默认数字或字符串

示例:

const L = require('1liner');

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

obj.query('proposer.country.default(GB)'); // "GB"
obj.query('proposer.country.default("GB")'); // "GB"
obj.query('proposer.height.default(180)'); // 180
obj.query('proposer.first_names.default("bob")'); // "John"

Multiple Queries

返回多个查询的结果。 查询需要返回数字作为值,否则会抛出错误。 查询也可以用数字代替。

多查询选项是最小值、最大值或范围。

示例:

const L = require('1liner');

const obj = new L({
    proposer: {
        title: "MR",
        first_names: "John",
        last_names: "Smith",
        age: 33,
        height: 180,
        weight: 100,
        convictions: [{
            code: "SP50",
            points: 4
        },
        {
            code: "SP50",
            points: 2
        }]
    }    
});

obj.query('min([proposer.weight, proposer.height])'); // 100
obj.query('max([proposer.weight, proposer.height])'); // 180
obj.query('min([100, proposer.height])'); // 100
obj.query('max([100, proposer.height])'); // 180
obj.query('range([proposer.weight, proposer.height])'); // 80

each.

根据对每个子查询的查询响应返回结果数组。 如果您希望聚合每个子节点而不是全部在一起,这将很有用。 每个 (each.) 只能用作第一个段声明(即查询字符串的第一部分)并且不能被 min、max 或 range 包裹,因为结果可以是整数、字符串或嵌套数组。

示例:

const L = require('1liner');

const obj = new L({
    additional_drivers: [{
        title: "MR",
        first_names: "John",
        last_names: "Smith",
        age: 33,
        height: 180,
        weight: 100,
        convictions: [{
            code: "SP50",
            points: 4
        },
        {
            code: "SP50",
            points: 2
        }]
    },{
        title: "MRS",
        first_names: "Jane",
        last_names: "Smith",
        age: 30,
        height: 120,
        weight: 60,
        convictions: [{
            code: "SP10",
            points: 0
        }]
    }]    
});

obj.query('each.additional_drivers.map(convictions).count()'); // [2, 1]
obj.query('additional_drivers.map(convictions).count()'); // 3
obj.query('each.additional_drivers.map(convictions).map(code)'); // [['SP50', 'SP50'], ['SP10']]
obj.query('additional_drivers.map(convictions).map(code)'); // ['SP50', 'SP50', 'SP10']

Testing

Node.js

安装开发依赖项:

$ npm install 1liner --development

然后导航到安装目录:

$ cd node_modules/1liner/

运行测试包:

$ 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
npm publish

Contributions

如果您为此库做出贡献,只需修改 index.jsindex.spec.js ,并更新 README.md。 我将更新网站文档并生成新的 dist/index.js、变更日志和版本。

Alternative packages

以下包允许对 JSON 进行更复杂的查询。

License

根据 GNU GENERAL PUBLIC LICENSE 获得许可。

版权所有 (C) 2020 Callum Rimmer callum@deadtrendy.co.uk

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: "John",
        last_names: "Smith",
        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: "John",
        last_names: "Smith",
        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, sum

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: "John",
        last_names: "Smith",
        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
obj.query('proposer.convictions.sum(points)'); // 6

/**
 * 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: "John",
        last_names: "Smith",
        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: "John",
        last_names: "Smith",
        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: "John",
        last_names: "Smith",
        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: "John",
        middle_name: "",
        last_names: "Smith",
        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"

- default

If property is undefined then it will return a defined default number or string

Example:

const L = require('1liner');

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

obj.query('proposer.country.default(GB)'); // "GB"
obj.query('proposer.country.default("GB")'); // "GB"
obj.query('proposer.height.default(180)'); // 180
obj.query('proposer.first_names.default("bob")'); // "John"

Multiple Queries

Returns an result from multiple queries. The queries need to return numbers as values otherwise an error with be thrown. The queries can be replaced with numbers too.

Multi-query options are min, max or range.

Example:

const L = require('1liner');

const obj = new L({
    proposer: {
        title: "MR",
        first_names: "John",
        last_names: "Smith",
        age: 33,
        height: 180,
        weight: 100,
        convictions: [{
            code: "SP50",
            points: 4
        },
        {
            code: "SP50",
            points: 2
        }]
    }    
});

obj.query('min([proposer.weight, proposer.height])'); // 100
obj.query('max([proposer.weight, proposer.height])'); // 180
obj.query('min([100, proposer.height])'); // 100
obj.query('max([100, proposer.height])'); // 180
obj.query('range([proposer.weight, proposer.height])'); // 80

each.

Returns an array of results based on the query responses to each of the children queries. This is useful if you wish to aggregate each child node instead of all together. Each (each.) can only be used as the first segment declaration (i.e. first part of the query string) and can not be wrapped by min, max or range as the result can be integers, strings or nested arrays.

Example:

const L = require('1liner');

const obj = new L({
    additional_drivers: [{
        title: "MR",
        first_names: "John",
        last_names: "Smith",
        age: 33,
        height: 180,
        weight: 100,
        convictions: [{
            code: "SP50",
            points: 4
        },
        {
            code: "SP50",
            points: 2
        }]
    },{
        title: "MRS",
        first_names: "Jane",
        last_names: "Smith",
        age: 30,
        height: 120,
        weight: 60,
        convictions: [{
            code: "SP10",
            points: 0
        }]
    }]    
});

obj.query('each.additional_drivers.map(convictions).count()'); // [2, 1]
obj.query('additional_drivers.map(convictions).count()'); // 3
obj.query('each.additional_drivers.map(convictions).map(code)'); // [['SP50', 'SP50'], ['SP10']]
obj.query('additional_drivers.map(convictions).map(code)'); // ['SP50', 'SP50', 'SP10']

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
npm publish

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

更多

友情链接

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文