File: API.md

package info (click to toggle)
node-json-stringify-safe 5.0.1%2Brepack-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,108 kB
  • sloc: javascript: 6,398; makefile: 117
file content (553 lines) | stat: -rw-r--r-- 17,270 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
Oolong.js API Documentation
===========================
### [Oolong](#Oolong)
- [.assign](#Oolong.assign)(target, source...)
- [.assignOwn](#Oolong.assignOwn)(target, source...)
- [.clone](#Oolong.clone)(object)
- [.cloneDeep](#Oolong.cloneDeep)(object)
- [.create](#Oolong.create)(prototype, [source...])
- [.defaults](#Oolong.defaults)(target, source...)
- [.defineGetter](#Oolong.defineGetter)(object, property, fn)
- [.defineSetter](#Oolong.defineSetter)(object, property, fn)
- [.each](#Oolong.each)(object, callback, [thisArg])
- [.eachOwn](#Oolong.eachOwn)(object, callback, [thisArg])
- [.filter](#Oolong.filter)(object, callback, [thisArg])
- [.forEach](#Oolong.forEach)(object, callback, [thisArg])
- [.forEachOwn](#Oolong.forEachOwn)(object, callback, [thisArg])
- [.has](#Oolong.has)(object, key)
- [.hasOwn](#Oolong.hasOwn)(object, key)
- [.isEmpty](#Oolong.isEmpty)(object)
- [.isIn](#Oolong.isIn)(object, key)
- [.isInOwn](#Oolong.isInOwn)(object, key)
- [.isObject](#Oolong.isObject)(object)
- [.isOwnEmpty](#Oolong.isOwnEmpty)(object)
- [.isPlainObject](#Oolong.isPlainObject)(object)
- [.keys](#Oolong.keys)(object)
- [.lookupGetter](#Oolong.lookupGetter)(object, property)
- [.lookupSetter](#Oolong.lookupSetter)(object, property)
- [.map](#Oolong.map)(object, callback, [thisArg])
- [.mapKeys](#Oolong.mapKeys)(object, callback, [thisArg])
- [.merge](#Oolong.merge)(target, source...)
- [.object](#Oolong.object)(keys, callback, [thisArg])
- [.ownKeys](#Oolong.ownKeys)(object)
- [.pick](#Oolong.pick)(object, keys...)
- [.pickDeep](#Oolong.pickDeep)(object, keys...)
- [.pluck](#Oolong.pluck)(object, key)
- [.property](#Oolong.property)(key)
- [.reject](#Oolong.reject)(object, callback, [thisArg])
- [.setPrototypeOf](#Oolong.setPrototypeOf)(object, prototype)
- [.values](#Oolong.values)(object)
- [.wrap](#Oolong.wrap)(value, key)


<a name="Oolong" />
Oolong
------


<a name="Oolong.assign" />
### Oolong.assign(target, source...)
Assigns all enumerable properties on `source` objects to `target`.  
Similar to `Object.assign`, but takes inherited properties into account.
Does not modify anything in the source objects.  
Returns `target`.

Think of it as _extending_ the first object step by step with others.

**Examples**:
```javascript
Oolong.assign({name: "John"}, {age: 32}, {shirt: "blue"})
// => {name: "John", age: 32, shirt: "blue"}
```

<a name="Oolong.assignOwn" />
### Oolong.assignOwn(target, source...)
Assigns all own enumerable properties on `source` objects to `target`.  
Like `Object.assign`. Does not modify anything in the source objects.  
Returns `target`.

Think of it as _extending_ the first object step by step with others.

**Examples**:
```javascript
Oolong.assignOwn({name: "John"}, {age: 32}, Object.create({shirt: "blue"}))
// => {name: "John", age: 32}
```

<a name="Oolong.clone" />
### Oolong.clone(object)
Creates a shallow clone of the given object, taking all enumerable
properties into account.  
Shallow means if you've got nested objects, those will be shared.

**Examples**:
```javascript
Oolong.clone({name: "John", age: 32})
// => {name: "John", age: 32}
```

<a name="Oolong.cloneDeep" />
### Oolong.cloneDeep(object)
Creates a deep clone of the given object, taking all enumerable properties
into account.

**Examples**:
```javascript
Oolong.cloneDeep({name: "John", attributes: {age: 42}})
// => {name: "John", attributes: {age: 42}}
```

<a name="Oolong.create" />
### Oolong.create(prototype, [source...])
Creates and returns an object inheriting from `prototype` and, optionally,
assigns enumerable properties from `source` objects to the new object.  
Uses `Object.create` and [`Oolong.assign`](#Oolong.assign)
internally.  
Does not modify the given `prototype` nor source objects.

**Examples**:
```javascript
var PERSON = {name: "Unknown", age: 0}
Oolong.create(PERSON, {name: "John"}, {shirt: "blue"})
// => {name: "John", age: 0, shirt: "blue"}
```

<a name="Oolong.defaults" />
### Oolong.defaults(target, source...)
Assigns all enumerable properties on `source` objects to `target` that the
`target` already _doesn't_ have. Uses `key in obj` to check for existence.  
Does not modify anything in the source objects.  
Returns `target`.

Note that because **inherited properties** on `target` are checked, any
property that exists on `Object.prototype` (e.g. `toString`, `valueOf`)
will be skipped. Usually that's not a problem, but if you want to use
`Oolong.defaults` for hashmaps/dictionaries with unknown keys, ensure
`target` inherits from `null` instead (use `Object.create(null)`).

**Examples**:
```javascript
var PERSON = {name: "Unknown", age: 0, shirt: "blue"}
Oolong.defaults({name: "John", age: 42}, PERSON)
// => {name: "John", age: 42, shirt: "blue"}
```

<a name="Oolong.defineGetter" />
### Oolong.defineGetter(object, property, fn)
Defines a getter on an object.  
Similar to [`Object.prototype.__defineGetter__`][__defineGetter__], but
works in a standards compliant way.  
Returns `object`.

The property is by default made *configurable* and *enumerable*. Should the
property exist before, it's enumerability will be left as is.

[__defineGetter__]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/__defineGetter__

**Examples**:
```javascript
var person = {birthyear: 1987}

Oolong.defineGetter(person, "age", function() {
  return new Date().getFullYear() - this.birthyear
})

person.age // => 28 as of today in 2015.
```

<a name="Oolong.defineSetter" />
### Oolong.defineSetter(object, property, fn)
Defines a setter on an object.  
Similar to [`Object.prototype.__defineSetter__`][__defineSetter__], but
works in a standards compliant way.  
Returns `object`.

The property is by default made *configurable* and *enumerable*. Should the
property exist before, it's enumerability will be left as is.

[__defineSetter__]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/__defineSetter__

**Examples**:
```javascript
var person = {}

Oolong.defineSetter(person, "age", function(age) {
  this.birthyear = new Date().getFullYear() - age
})

person.age = 28
person.birthyear // => 1987 as of today in 2015.
```

<a name="Oolong.each" />
### Oolong.each(object, callback, [thisArg])
Calls the given function for all enumerable properties.  
Returns the given object.

The function will be called with arguments `value`, `key` and `object` and
bound to `thisArg`.

**Examples**:
```javascript
var obj = {name: "John", age: 42}
Oolong.each(obj, function(val, key) { console.log(key + "=" + val) })
```

<a name="Oolong.eachOwn" />
### Oolong.eachOwn(object, callback, [thisArg])
Calls the given function for all _own_ enumerable properties.  
Returns the given object.

The function will be called with arguments `value`, `key` and `object` and
bound to `thisArg`.

**Examples**:
```javascript
var obj = {name: "John", age: 42}
Oolong.eachOwn(obj, function(val, key) { console.log(key + "=" + val) })
```

<a name="Oolong.filter" />
### Oolong.filter(object, callback, [thisArg])
Filters all enumerable properties and returns a new object with only those
properties for which the given function returned truthy for.

The function will be called with arguments `value`, `key` and `object` and
bound to `thisArg`.

**Examples**:
```javascript
var obj = {a: 1, b: 2, c: 3, d: 4}
Oolong.filter(obj, function(value, key) { return value % 2 == 0 })
// => {b: 2, d: 4}
```

<a name="Oolong.forEach" />
### Oolong.forEach(object, callback, [thisArg])
Alias of [`each`](#Oolong.each).  

<a name="Oolong.forEachOwn" />
### Oolong.forEachOwn(object, callback, [thisArg])
Alias of [`eachOwn`](#Oolong.eachOwn).  

<a name="Oolong.has" />
### Oolong.has(object, key)
Checks whether the given object has the given property, inherited or not.  
Given a set, but `undefined` property will still return `true`.

**Examples**:
```javascript
Oolong.has({name: "John"}) // => true
Oolong.has(Object.create({name: "John"}), "name") // => true
Oolong.has({}, "name") // => false
```

<a name="Oolong.hasOwn" />
### Oolong.hasOwn(object, key)
Checks whether the given object has the given property as an own property.  
Given a set, but `undefined` property will still return `true`.

**Examples**:
```javascript
Oolong.hasOwn({name: "John"}) // => true
Oolong.hasOwn(Object.create({name: "John"}), "name") // => false
Oolong.hasOwn({}, "name") // => false
```

<a name="Oolong.isEmpty" />
### Oolong.isEmpty(object)
Checks whether the given object has any enumerable properties, inherited
or not.

**Examples**:
```javascript
Oolong.isEmpty({name: "John"}) // => false
Oolong.isEmpty(Object.create({name: "John"})) // => false
Oolong.isEmpty({}) // => true
```

<a name="Oolong.isIn" />
### Oolong.isIn(object, key)
Alias of [`has`](#Oolong.has).  

<a name="Oolong.isInOwn" />
### Oolong.isInOwn(object, key)
Alias of [`hasOwn`](#Oolong.hasOwn).  

<a name="Oolong.isObject" />
### Oolong.isObject(object)
Checks whether the given object is of type object and is not null.

**Examples**:
```javascript
Oolong.isObject({name: "John"}) // => true
Oolong.isObject(new Date) // => true
Oolong.isObject(42) // => false
Oolong.isObject(null) // => false
```

<a name="Oolong.isOwnEmpty" />
### Oolong.isOwnEmpty(object)
Checks whether the given object has any _own_ enumerable properties.

**Examples**:
```javascript
Oolong.isOwnEmpty({name: "John"}) // => false
Oolong.isOwnEmpty(Object.create({name: "John"})) // => true
Oolong.isOwnEmpty({}) // => true
```

<a name="Oolong.isPlainObject" />
### Oolong.isPlainObject(object)
Checks whether the given object is one constructed by `Object` or inheriting
from `null`.

A non-plain object has a `constructor` property set to anything but `Object`.
That's the case when you do, for example, `new MyModel`, `new Date`.

`Array.prototype` is not considered a plain object just like an array isn't
a plain object. JavaScript is a prototypical language and the prototype of
an array should be considered an array.

**Examples**:
```javascript
Oolong.isPlainObject({name: "John", age: 42}) // => true
Oolong.isPlainObject(Object.create(null)) // => true
Oolong.isPlainObject(Math) // => true
Oolong.isPlainObject([]) // => false
Oolong.isPlainObject(Array.prototype) // => false
Oolong.isPlainObject(new Date) // => false
Oolong.isPlainObject("John") // => false
```

<a name="Oolong.keys" />
### Oolong.keys(object)
Returns all enumerable keys of an object as an array.
Similar to `Object.keys`, but takes inherited properties into account.

**Examples**:
```javascript
Oolong.keys({name: "John", age: 32}) // => ["name", "age"]
```

<a name="Oolong.lookupGetter" />
### Oolong.lookupGetter(object, property)
Looks up and returns a getter on an object.  
Similar to [`Object.prototype.__lookupGetter__`][__lookupGetter__], but
works in a standards compliant way.  
Takes inherited getters into account, just like `__lookupGetter__`.  

[__lookupGetter__]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/__lookupGetter__

**Examples**:
```javascript
var person = {birthyear: 1987}

Oolong.defineGetter(person, "age", function() {
  return new Date().getFullYear() - this.birthyear
})

Oolong.lookupGetter(person, "age") // Returns the function above.
```

<a name="Oolong.lookupSetter" />
### Oolong.lookupSetter(object, property)
Looks up and returns a setter on an object.  
Similar to [`Object.prototype.__lookupSetter__`][__lookupSetter__], but
works in a standards compliant way.  
Takes inherited setters into account, just like `__lookupSetter__`.  

[__lookupSetter__]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/__lookupSetter__

**Examples**:
```javascript
var person = {birthyear: 1987}

Oolong.defineSetter(person, "age", function(age) {
  this.birthyear = new Date().getFullYear() - age
})

Oolong.lookupSetter(person, "age") // Returns the function above.
```

<a name="Oolong.map" />
### Oolong.map(object, callback, [thisArg])
Maps all enumerable property values and returns a new object.

The function will be called with arguments `value`, `key` and `object` and
bound to `thisArg`.

**Examples**:
```javascript
var obj = {a: 1, b: 2, c: 3}
Oolong.map(obj, function(value, key) { return value * 2 })
// => {a: 2, b: 4, c: 6}
```

<a name="Oolong.mapKeys" />
### Oolong.mapKeys(object, callback, [thisArg])
Transforms all enumerable keys and returns a new object.

The function will be called with arguments `key`, `value` and `object` and
bound to `thisArg`.

**Examples**:
```javascript
var person = {name: "John", age: 32}
Oolong.mapKeys(person, function(key) { return key.toUpperCase() })
// => {NAME: "John", AGE: 32}
```

<a name="Oolong.merge" />
### Oolong.merge(target, source...)
Assigns all enumerable properties on `source` objects to `target`
recursively.  
Only plain objects a merged. Refer to
[`Oolong.isPlainObject`](#Oolong.isPlainObject) for the definition of
a plain object. Does not modify anything in the source objects.

Think of it as _extending_ the first object step by step with others.

**Examples**:
```javascript
var person = {name: "John", attributes: {age: 42}}
Oolong.merge(person, {attributes: {height: 190}})
person // => {name: "John", attributes: {age: 42, height: 190}}
```

<a name="Oolong.object" />
### Oolong.object(keys, callback, [thisArg])
Returns a new object with keys taken from the array `keys` and values
from the result of calling the given function with `key`, `index` and
`keys`.  
It's like the reverse of indexing an array.

**Examples**:
```javascript
var names = ["Alice", "Bob", "Charlie"]
var lengths = Oolong.object(names, function(name) { return name.length })
lengths // => {Alice: 5, Bob: 3, Charlie: 7}
```

<a name="Oolong.ownKeys" />
### Oolong.ownKeys(object)
Returns all enumerable _own_ keys of an object as an array.  
Same as `Object.keys`, really.

**Examples**:
```javascript
var person = Object.create({name: "John"})
person.age = 42
Oolong.ownKeys(person) // => ["age"]
```

<a name="Oolong.pick" />
### Oolong.pick(object, keys...)
Filters the keys of an object to only those given as `keys...`.  
Only keys that exist in `object` are included.

**Examples**:
```javascript
var person = {name: "Alice", email: "alice@example.com", age: 42}
Oolong.pick(person, "name", "age") // => {name: "Alice", age: 42}
```

<a name="Oolong.pickDeep" />
### Oolong.pickDeep(object, keys...)
Filters the keys of an object to only those given as `keys...` with support
for nested keys in an array (`["a", "b", "c"]`).  
Only keys that exist in `object` are included.

If you'd like to use some other path syntax, feel free to preprocess your
keys before passing them to `pickDeep`. For example, for a period-separated
syntax (`a.b.c`), use a helper:

```javascript
function path(s) { return s.split(".") }
Oolong.pickDeep(person, "name", path("address.country"))
```

**Examples**:
```javascript
var person = {name: "Alice", address: {country: "UK", street: "Downing"}}
var obj = Oolong.pickDeep(person, "name", ["address", "country"])
obj // => {name: "Alice", address: {country: "UK"}}
```

<a name="Oolong.pluck" />
### Oolong.pluck(object, key)
Returns a new object with the same keys, but with values being the value's
property `key`.  
In other words, it's the same as `Oolong.map(obj, Oolong.property(key))`.

**Examples**:
```javascript
var people = {
  a: {name: "Alice"},
  b: {name: "Bob"},
  c: {name: "Charlie"}
}

Oolong.pluck(people, "name") // => {a: "Alice", b: "Bob", c: "Charlie"}
```

<a name="Oolong.property" />
### Oolong.property(key)
Returns a function that returns the given property of an object.

**Examples**:
```javascript
var getName = Oolong.property("name")
getName({name: "John"}) // => "John
```

<a name="Oolong.reject" />
### Oolong.reject(object, callback, [thisArg])
Rejects all enumerable properties and returns a new object without those
properties for which the given function returned truthy for.  
Opposite of [`filter`](#Oolong.filter).

The function will be called with arguments `value`, `key` and `object` and
bound to `thisArg`.

**Examples**:
```javascript
var obj = {a: 1, b: 2, c: 3, d: 4}
Oolong.reject(obj, function(value, key) { return value % 2 == 0 })
// => {a: 1, c: 3}
```

<a name="Oolong.setPrototypeOf" />
### Oolong.setPrototypeOf(object, prototype)
Set the prototype of the given object to the given prototype.  
Pass `null` or another object for the prototype.  
Returns `object`.

Uses `Object.setPrototypeOf` if it exists. Otherwise uses a polyfill.

**Examples**:
```javascript
var person = {name: "Unnamed", age: 42}
var mike = Oolong.setPrototypeOf({name: "Mike"}, person)
mike.name // => "Mike
mike.age  // => 42
```

<a name="Oolong.values" />
### Oolong.values(object)
Returns all enumerable property values as an array.

**Examples**:
```javascript
Oolong.values({name: "John", age: 32}) // => ["John", 32]
```

<a name="Oolong.wrap" />
### Oolong.wrap(value, key)
Wraps a given value in an object under the specified key.  
Works also with [ECMAScript 6 Symbol](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Symbol).

**Examples**:
```javascript
Oolong.wrap("John", "name") // => {name: "John"}
```