UNPKG

5.03 kBMarkdownView Raw
1# K2
2
3> Functional javascript utilities
4
5[![NPM][k2-icon] ][k2-url]
6
7[![Circle CI](https://circleci.com/gh/kensho/k2.svg?style=svg)](https://circleci.com/gh/kensho/k2)
8[![Code Climate](https://codeclimate.com/github/kensho/k2/badges/gpa.svg)](https://codeclimate.com/github/kensho/k2)
9
10[k2-icon]: https://nodei.co/npm/k2.png?downloads=true
11[k2-url]: https://npmjs.org/package/k2
12
13## Install
14
15 npm install --save k2
16
17In the browser, if you already have global [lodash](https://lodash.com/docs) library, you can
18include `dist/k2-browser.js`. Under Node, or to use lodash included in the k2 bundle, use `dist/k2.js`.
19
20```js
21// node
22var k2 = require('k2');
23```
24
25## Api
26
27### presentProperties
28
29Determines if each property in the list is present in each object given in the second list
30
31```js
32var bad = [{
33 name: 'foo',
34 age: 1
35}, {
36 name: 'bar'
37}, {
38 name: 'baz'
39}];
40presentProperties(['name', 'age'], bad);
41// ['name']
42```
43
44`presentProperties` is curried, works with deep properties, requires `lodash` version
45with deep property support, see [_.has](https://lodash.com/docs#has)
46
47### cleanEnteredText
48
49Removes HTML entities from user-entered text. Common entities introduced by a textarea element
50are ` ` at the end.
51
52```js
53var cleanText = k2.cleanEnteredText(text);
54// text - string entered into a textarea
55```
56
57```js
58k2.cleanEnteredText('FOO b') // 'foo b'
59k2.cleanEnteredText('foo ') // 'foo'
60```
61
62### findPartialMatches
63
64Finds all items where given properties are matching the given query text.
65
66```js
67var matches = k2.findPartialMatches(properties, items, queryText);
68// properties - single string or an array of strings
69// items - objects to search over
70// queryText - single string
71```
72
73```js
74// find all items where property 'foo' contains 'ooo'
75var items = [{
76 foo: 'foo'
77}, {
78 foo: 'foo2'
79}, {
80 foo: 'bar'
81}];
82var result = k2.findPartialMatches('foo', items, 'oo');
83// result is [ items[0], items[1] ]
84```
85
86### rankPartialMatches
87
88Useful to order items by text entered by the user, but only considering matches of query text to
89certain properties. Items without any matches will be pushed to the back of the returned list.
90
91```js
92var matches = k2.rankPartialMatches(properties, matches, queryText);
93// properties - single string or array of strings
94// matches - objects to search
95// queryText - single string
96```
97
98```js
99var items = [{
100 foo: 'foo'
101}, {
102 foo: 'foo2',
103 bar: 'abar'
104}, {
105 foo: 'bar'
106}];
107var result = k2.rankPartialMatches(['foo', 'bar'], items, 'bar');
108// result is a new array with 3 items
109// result[0] is items[2] (exact match)
110// result[1] is items[1] (partial match)
111// result[2] is items[0] (nothing matches)
112```
113
114### objectLens
115
116A function for building immutable object lenses. Construct the lens with a key
117and then use its getter, setter, or mapper.
118
119```js
120objectLens('name')({ name: 'joe' });
121// => 'joe'
122objectLens('age').set(20, { age: 19 });
123// => { age: 20 }
124objectLens('age').map(R.add(1), { age: 19 });
125// => { age: 20 }
126```
127
128To compose setters, use function composition!
129
130```js
131var setter = R.compose(objectLens('name').set('matt'),
132 objectLens('age').map(R.add(1)));
133setter({ name: 'joe', age: 19 })
134// => { name: 'matt', age: 20 }
135```
136
137### onlyTrue
138
139XOR for predicates - returns true if and only if a single value is truthy.
140
141```js
142onlyTrue(true, false, false); // true
143onlyTrue(false, false, false); // false
144onlyTrue(false, true, true); // false
145onlyTrue(false, false, true); // true
146```
147
148### guessDateFormat
149
150Tries to determine date format from single or list of strings. If there is ambiguity returns undefined.
151
152```js
153guessDateFormat('2010-15-10'); // 'YYYY-DD-MM'
154guessDateFormat(['05/19/2000', '22/01/2002']); // undefined
155```
156
157### Small print
158
159Author: Kensho © 2015
160
161* [@kensho](https://twitter.com/kensho)
162* [kensho.com](http://kensho.com)
163
164Support: if you find any problems with this library,
165[open issue](https://github.com/kensho/k2/issues) on Github
166
167## MIT License
168
169The MIT License (MIT)
170
171Copyright (c) 2015 Kensho
172
173Permission is hereby granted, free of charge, to any person obtaining a copy of
174this software and associated documentation files (the "Software"), to deal in
175the Software without restriction, including without limitation the rights to
176use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
177the Software, and to permit persons to whom the Software is furnished to do so,
178subject to the following conditions:
179
180The above copyright notice and this permission notice shall be included in all
181copies or substantial portions of the Software.
182
183THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
184IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
185FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
186COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
187IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
188CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
189