当前位置:主页 > 资料 >

Kind of like GraphQL except for Javascript objects .. extrac
栏目分类:资料   发布日期:2018-08-03   浏览次数:

导读:本文为去找网小编(www.7zhao.net)为您推荐的Kind of like GraphQL except for Javascript objects .. extracts/transforms sub-objects.,希望对您有所帮助,谢谢! doxl v0.1.1 Kind of like GraphQL except for Javascript objec

本文为去找网小编(www.7zhao.net)为您推荐的Kind of like GraphQL except for Javascript objects .. extracts/transforms sub-objects.,希望对您有所帮助,谢谢!

去找(www.7zhao.net欢迎您



doxl v0.1.1

Kind of like GraphQL except for Javascript objects. Extracts and optionally transforms sub-objects from Javascript objects. Just 640 bytes compressed and gzipped.

欢迎访问www.7zhao.net

Installation

npm install doxl

内容来自www.7zhao.net

The browser version does not require transpiling but exists at browser/doxl.js .

本文来自去找www.7zhao.net

Usage

Either include doxl.js through an import stamement, script tag or require statement. Then it is pretty darn simple ... 去找(www.7zhao.net欢迎您

Just call doxl(query,source) where query and source are both JavaScript objects. The value returned will be the subset of source, if any, that has the found properties from the query such that they have at least one of the following: www.7zhao.net

  1. an exact match for value

    copyright www.7zhao.net

  2. satisfied a function test provided in the query, i.e. function returns truthy or null or zero, not undefined or false. 内容来自www.7zhao.net

  3. have a value that results from calling a function on the source with the query property value as arguments

    www.7zhao.net

const match = doxl({name:doxl.ANY,age:value => value >= 21},
					 {name:"joe",age:21,gender:"male",address:{city:"seattle"}}); 

欢迎访问www.7zhao.net

will return

去找(www.7zhao.net欢迎您

{name:"joe",age:21,address:{city:"seattle"}}} 欢迎访问www.7zhao.net 

API

doxl(query,source,{partial,constructorMatch,transform,schema}={}) -

欢迎访问www.7zhao.net

query - An object, possibly nested, that contains properties to extract and values to literally match or functions to test for a match. 欢迎访问www.7zhao.net

source - An object from which to extract data. Any functions on the source object will be called with the arguments (...queryValue) , with this bound to source . For fat arrow functions, source is not available. Mapping single arguments into the array required for ...queryValue is done automatically. If your function takes a single argument that is an array, then you must nest it one level, e.g. f(someArray) should be use the query {f:[matchingArray]} not {f:matchingArray} . 内容来自www.7zhao.net

partial - The default behavior is to return only full matches. If partial is truthy, then a value will returned for any properties match.

欢迎访问www.7zhao.net

constructorMatch - Typically the query and source will be POJOs and with the exception of Array , Date , RegExp , Map , and Set the class of a source or its nested objects is ignored. If constructorMatch is truthy, then the constructors for the query and source must match.

内容来自www.7zhao.net

transform - Typically any functions in the query are treates as predicated that return a truthy or falsy value; however, if transform is truthy then these functions can consume the value from the source and return the same or a different value. If the value returned is undefined , then it is assumed no match occured. 欢迎访问www.7zhao.net

schema - Reserved for future use.

欢迎访问www.7zhao.net

doxl.ANY - A utility function defined as () => true that will match any value.

copyright www.7zhao.net

doxl.UNDEFINEDOK(default) - A utility function that will match undefined properties in the source . If default is provided, it will be returned as the value for the undefined property. 内容来自www.7zhao.net

Application Techniques

Handling undefined

A source can have an undefined property and still have a successful match by using doxl.UNDEFINEDOK . www.7zhao.net

doxl({name:olx.ANY,age:doxl.ANY,gender:doxl.UNDEFINEDOK()},{age:21,name:"joe"}); 内容来自www.7zhao.net 

will match: 内容来自www.7zhao.net

{name:"joe",age:21} 本文来自去找www.7zhao.net 

And,

内容来自www.7zhao.net

doxl({name:olx.ANY,age:doxl.ANY,gender:doxl.UNDEFINEDOK()},{age:21,name:"joe",gender:"male"}); 
www.7zhao.net

will match:

copyright www.7zhao.net

{name:"joe",age:21,gender:"male"} 内容来自www.7zhao.net 

Whereas,

欢迎访问www.7zhao.net

doxl({name:olx.ANY,age:doxl.ANY,gender:doxl.UNDEFINEDOK("undeclared")},{age:21,name:"joe"}); 去找(www.7zhao.net欢迎您 

will match: 去找(www.7zhao.net欢迎您

{name:"joe",age:21,gender:"undeclared"} 内容来自www.7zhao.net 

Processing Arrays Of Possible Matches

Assume you have an array of objects you wish to search, you can reduce it using reduce and doxl : copyright www.7zhao.net

[{name:"joe",age:21,employed:true},
 {name:"mary",age:20,employed:true},
 {name:"jack",age:22,employed:false}
].reduce(item => {
	const match = doxl({name:doxl.ANY,age:value => value >= 21,employed:false},item,{all:true}));
	if(match) accum.push(match);
	return accum;
},[]); 

内容来自www.7zhao.net

will match:

本文来自去找www.7zhao.net

{name:"jack",age:22,employed:false} 

www.7zhao.net

Re-Ordering Keys

doxl({name:olx.ANY,age:doxl.ANY},{age:21,name:"joe"}); 

内容来自www.7zhao.net

will return:

内容来自www.7zhao.net

{name:"joe",age:21} copyright www.7zhao.net 

Dynamic Property Values

If your source objects are class instances with methods or objects containing functions, they will get resolved:

www.7zhao.net

class Person {
			constructor({firstName,lastName,favoriteNumbers=[]}) {
				this.firstName = firstName;
				this.lastName = lastName;
				this.favoriteNumbers = favoriteNumbers;
			}
			name() {
				return `${this.lastName}, ${this.firstName}`;
			}
			someFavoriteNumber(number) {
				if(this.favoriteNumbers.includes(number)) {
					return number;
				}
			}
		}

		const people = [
				new Person({firstName:"joe",lastName:"jones",favoriteNumbers:[5,15]}),
				new Person({firstName:"mary",lastName:"contrary",favoriteNumbers:[7,14]})
				];

		const matches = people.reduce((accum,item) => { 
				const match = doxl({name:doxl.ANY,someFavoriteNumber:7},item,{all:true});
				if(match) accum.push(match);
				return accum;
			},[]); copyright www.7zhao.net 

will match:

copyright www.7zhao.net

{name:"contrary, mary",someFavoriteNumber:7} 
内容来自www.7zhao.net

Transformations

If the option transform is truthy, object transformations can occur:

copyright www.7zhao.net

doxl({size: value => size * 2},{size: 2},{transform:true}); 本文来自去找www.7zhao.net 

results in:

本文来自去找www.7zhao.net

{size: 4} 
内容来自www.7zhao.net

Why doxl

Most other extraction and transformation libraries require specifying critical specification as strings that need to be parsed increasing the library size and the chance for typographical errors. In the extreme case they for the developer to learn an entirely new syntax and semantics. The doxl library is small and pure Javascript.

去找(www.7zhao.net欢迎您

Release History (reverse chronological order)

2018-08-02 v0.1.1 Documentation updates. www.7zhao.net

2018-08-02 v0.1.0 First public release as independent module.

内容来自www.7zhao.net

License

MIT License 去找(www.7zhao.net欢迎您

Copyright (c) 2018 Simon Y. Blackwell, AnyWhichWay, LLC

欢迎访问www.7zhao.net

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 本文来自去找www.7zhao.net

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

www.7zhao.net

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 本文来自去找www.7zhao.net

www.7zhao.net


本文原文地址:https://github.com/anywhichway/doxl

以上为Kind of like GraphQL except for Javascript objects .. extracts/transforms sub-objects.文章的全部内容,若您也有好的文章,欢迎与我们分享!

copyright www.7zhao.net

Copyright ©2008-2017去找网版权所有   皖ICP备12002049号-2 皖公网安备 34088102000435号   关于我们|联系我们| 免责声明|友情链接|网站地图|手机版