UNPKG

4.34 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## Api
14
15### cleanEnteredText
16
17Removes HTML entities from user-entered text. Common entities introduced by a textarea element
18are ` ` at the end.
19
20```js
21var cleanText = k2.cleanEnteredText(text);
22// text - string entered into a textarea
23```
24
25```js
26k2.cleanEnteredText('FOO b') // 'foo b'
27k2.cleanEnteredText('foo ') // 'foo'
28```
29
30### findPartialMatches
31
32Finds all items where given properties are matching the given query text.
33
34```js
35var matches = k2.findPartialMatches(properties, items, queryText);
36// properties - single string or an array of strings
37// items - objects to search over
38// queryText - single string
39```
40
41```js
42// find all items where property 'foo' contains 'ooo'
43var items = [{
44 foo: 'foo'
45}, {
46 foo: 'foo2'
47}, {
48 foo: 'bar'
49}];
50var result = k2.findPartialMatches('foo', items, 'oo');
51// result is [ items[0], items[1] ]
52```
53
54### rankPartialMatches
55
56Useful to order items by text entered by the user, but only considering matches of query text to
57certain properties. Items without any matches will be pushed to the back of the returned list.
58
59```js
60var matches = k2.rankPartialMatches(properties, matches, queryText);
61// properties - single string or array of strings
62// matches - objects to search
63// queryText - single string
64```
65
66```js
67var items = [{
68 foo: 'foo'
69}, {
70 foo: 'foo2',
71 bar: 'abar'
72}, {
73 foo: 'bar'
74}];
75var result = k2.rankPartialMatches(['foo', 'bar'], items, 'bar');
76// result is a new array with 3 items
77// result[0] is items[2] (exact match)
78// result[1] is items[1] (partial match)
79// result[2] is items[0] (nothing matches)
80```
81
82### objectLens
83
84A function for building immutable object lenses. Construct the lens with a key
85and then use its getter, setter, or mapper.
86
87```js
88objectLens('name')({ name: 'joe' });
89// => 'joe'
90objectLens('age').set(20, { age: 19 });
91// => { age: 20 }
92objectLens('age').map(R.add(1), { age: 19 });
93// => { age: 20 }
94```
95
96To compose setters, use function composition!
97
98```js
99var setter = R.compose(objectLens('name').set('matt'),
100 objectLens('age').map(R.add(1)));
101setter({ name: 'joe', age: 19 })
102// => { name: 'matt', age: 20 }
103```
104
105### onlyTrue
106
107XOR for predicates - returns true if and only if a single value is truthy.
108
109```js
110onlyTrue(true, false, false); // true
111onlyTrue(false, false, false); // false
112onlyTrue(false, true, true); // false
113onlyTrue(false, false, true); // true
114```
115
116### guessDateFormat
117
118Tries to determine date format from single or list of strings. If there is ambiguity returns undefined.
119
120```js
121guessDateFormat('2010-15-10'); // 'YYYY-DD-MM'
122guessDateFormat(['05/19/2000', '22/01/2002']); // undefined
123```
124
125### Small print
126
127Author: Kensho © 2015
128
129* [@kensho](https://twitter.com/kensho)
130* [kensho.com](http://kensho.com)
131
132Support: if you find any problems with this library,
133[open issue](https://github.com/kensho/k2/issues) on Github
134
135## MIT License
136
137The MIT License (MIT)
138
139Copyright (c) 2015 Kensho
140
141Permission is hereby granted, free of charge, to any person obtaining a copy of
142this software and associated documentation files (the "Software"), to deal in
143the Software without restriction, including without limitation the rights to
144use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
145the Software, and to permit persons to whom the Software is furnished to do so,
146subject to the following conditions:
147
148The above copyright notice and this permission notice shall be included in all
149copies or substantial portions of the Software.
150
151THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
152IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
153FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
154COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
155IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
156CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
157