UNPKG

5.29 kBJavaScriptView Raw
1
2/**
3 * Utility functions:
4 * (Section) These are utilities that don't need a wrapped object.
5 */
6
7/**
8 * scour.get : scour.get(object, keypath)
9 * Gets a keypath from an object.
10 *
11 * data = { users: { bob: { name: 'john' } } }
12 *
13 * result = get(data, ['users', 'bob', 'name'])
14 * // => 'robert'
15 *
16 * This is also available as `require('scourjs/utilities/get')`.
17 */
18
19exports.get = require('./get')
20
21/**
22 * scour.set : scour.set(object, keypath, value)
23 * Sets a `keypath` into an `object` immutably.
24 *
25 * data = { users: { bob: { name: 'john' } } }
26 *
27 * result = set(data, ['users', 'bob', 'name'], 'robert')
28 * // => { users: { bob: { name: 'robert' } } }
29 *
30 * This is also available as `require('scourjs/utilities/set')`.
31 */
32
33exports.set = require('./set')
34
35/**
36 * scour.del : scour.del(object, keypath)
37 * Deletes a `keypath` from an `object` immutably.
38 *
39 * data = { users: { bob: { name: 'robert' } } }
40 * result = del(data, ['users', 'bob', 'name'])
41 *
42 * // => { users: { bob: {} } }
43 *
44 * This is also available as `require('scourjs/utilities/del')`.
45 */
46
47exports.del = require('./del')
48
49/**
50 * scour.extendIn : scour.extendIn(object, keypath, extensions...)
51 * Extends a `keypath` from an `object` immutably.
52 *
53 * data = { users: { bob: { name: 'robert' } } }
54 * result = extendIn(data, ['users', 'bob'], { email: 'bob@gmail.com' })
55 *
56 * // => { users: { bob: { name: 'robert', email: 'bob@gmail.com' } } }
57 *
58 * This is also available as `require('scourjs/utilities/extend_in')`.
59 */
60
61exports.extendIn = require('./extend_in')
62
63/**
64 * scour.each : scour.each(iterable, fn)
65 * Iterates through `iterable`, either an object or an array. This is an
66 * implementation of [Array#forEach] that also works for objects. The callback
67 * `fn` will be invoked with two parameters: `currentValue` and `key`, just
68 * like `Array#forEach`.
69 *
70 * This is also available as `require('scourjs/utilities/each')`.
71 *
72 * [Array#forEach]: http://devdocs.io/javascript/global_objects/array/foreach
73 */
74
75exports.each = require('./each')
76
77/**
78 * scour.map : scour.map(iterable, fn)
79 * Creates a new `Array` with with the results of calling a provided function
80 * on every element in this array. Works like [Array#map], but also works on
81 * objects as well as arrays.
82 *
83 * The callback `fn` will be invoked with two parameters: `currentValue` and
84 * `key`, just like [Array#map].
85 *
86 * This is also available as `require('scourjs/utilities/map')`.
87 *
88 * [Array#map]: http://devdocs.io/javascript/global_objects/array/map
89 */
90
91exports.map = require('./map')
92
93/**
94 * scour.mapObject : scour.mapObject(iterable, fn)
95 * Creates a new `Object` with with the results of calling a provided function
96 * on every element in this array. Works like [Array#map], but also works on
97 * objects as well as arrays, and it returns an object instead.
98 *
99 * The callback `fn` will be invoked with two parameters: `currentValue` and
100 * `key`, just like [Array#map].
101 *
102 * object = { a: 20, b: 30, c: 40 }
103 * result = scour.mapObject(object, (val, key) => {
104 * return '$' + val + '.00'
105 * })
106 *
107 * // => { a: '$20.00', b: '$30.00', c: '$40.00' }
108 *
109 * This is also available as `require('scourjs/utilities/map_object')`.
110 */
111
112exports.mapObject = require('./map_object')
113
114/**
115 * scour.indexedMap : scour.indexedMap(iterable, fn)
116 * Creates a new `Object` with with the results of calling a provided function
117 * returning the keys and values for the new object.
118 *
119 * The callback `fn` will be invoked with two parameters: `currentValue` and
120 * `key`, just like [Array#map].
121 *
122 * The callback `fn` should return an array with two elements: with `result[0]`
123 * being the key, and `result[1]` being the value. These are what the new
124 * object will be constructed with.
125 *
126 * The `iterable` parameter can be an object or an array. This works like
127 * `Array#map`, but also works on objects as well as arrays.
128 *
129 * list = ['Fred', 'Barney', 'Wilma']
130 *
131 * object = scour.indexedMap(list, (val, key) => {
132 * var newkey = val.substr(0, 1)
133 * return [ newkey, val ]
134 * })
135 *
136 * // => { f: 'Fred', b: 'Barney', w: 'Wilma' }
137 *
138 * This is also available as `require('scourjs/utilities/indexed_map')`.
139 */
140
141exports.indexedMap = require('./indexed_map')
142
143/**
144 * scour.filter : scour.filter(iterable, function(val, key), [isArray])
145 * Creates a new Array or Object with all elements that pass the test
146 * implemented by the provided function.
147 *
148 * Works like [Array#filter], but will return an object if an object is also passed.
149 *
150 * The optional `isArray` argument, when passed `true`, will always make this
151 * return an `Array`. If `false`, it will always be an `Object`. Leave it
152 * `undefined` for the default behavior.
153 *
154 * This is also available as `require('scourjs/utilities/filter')`.
155 *
156 * [Array#filter]: http://devdocs.io/javascript/global_objects/array/filter
157 */
158
159exports.filter = require('./filter')
160
161/**
162 * scour.sortBy : scour.sortBy(iterable, criteria)
163 * Sorts by a given criteria.
164 *
165 * list = [ { name: 'Fred' }, { name: 'Barney' }, { name: 'Wilma' } ]
166 * scour.sortBy(list, 'name')
167 *
168 * This is also available as `require('scourjs/utilities/sort_by')`.
169 */
170
171exports.sortBy = require('./sort_by')