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