class NoDash
Defined in: main.js, line 152
Special {}
value that can be given to NoDash’s forEach() and others
to make them treat an isArrayLike value like an object.
Defined in: main.js, line 220 • Show code
forceObject: fo,
Underscore/LoDash
Defined in: main.js, line 2345 • Show code
isArray: Array.isArray,
ECMAScript (not in IE)
Defined in: main.js, line 2348 • Show code
sign: Math.sign || function (n) {
Returns an array of all keys of value
, including non-own.
Name | Types | Notes |
---|---|---|
value | object |
See also has(), values().
Warning: uses for..in
that is terribly slow for objects with
prototype chains (not just plain {}
).
from unordered
Attention: be wary about object value
– JavaScript objects are
unordered.
allKeys() returns keys in arbitrary order.
Defined in: main.js, lines 1951-1955 (5 lines) • Show code
allKeys: function (value) {
var res = []
for (var key in value) { res.push(key) }
return res
},
Merges members of given objects into the first argument, overwriting keys of earlier arguments with later ones.
Types | Notes |
---|---|
object first of given arguments |
Name | Types | Notes |
---|---|---|
objects | Only object-type arguments. |
ECMAScript equivalent: o:Object/assign (not in IE). See also union(), intersection().
Warning: assign() mutates the first argument (and returns it).
Only own properties are considered (has()):
_.assign({toString: f}, {})
//=> first argument unchanged, even though: 'toString' in {}
Defined in: main.js, line 809
Returns value of value
’s property or of its sub-objects.
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
path | array | Each member is a key; resolving
stops on null or undefined |
scalar assume [path] | ||
default | mixed returned if property not found | |
omitted returns the last
found undefined or null |
Without default
, it’s not possible to determine if path
has resolved
to a property with undefined
or null
or if it ended prematurely on
such a property with more components left in path
.
See also property() that returns a callback suitable for map().
Defined in: main.js, lines 2272-2281 (10 lines) • Show code
at: function (value, path, def) {
if (typeof path == 'string' || !NoDash.isArrayLike(path)) { path = [path] }
for (var i = 0; i < path.length; i++) {
if (value == null) {
return arguments.length > 2 && def !== unset ? def : value
}
value = value[path[i]]
}
return value
},
Returns a version of func
with added arguments and/or forced context.
Types | Notes |
---|---|
function | |
falsy if func or func[0] is falsy |
Name | Types | Notes |
---|---|---|
func | function (args are prepended) | |
array of func[, cx[,
...hereArgs]] (cx and args arguments are ignored) | ||
cx | object override the caller-specified context for func (its
this ) | |
undefined keep the caller-specified context | ||
args | Argument(s) pushed in front of the caller’s arguments to func |
ECMAScript equivalent: o:Function/bind.
bind() is used for non-ES utilities like sortBy() to normalize the
callback format for compatibility with Underscore, where some functions
accept cx
(un:findIndex), some accept arguments (un:defer),
some accept nothing (e.g. un:throttle).
If your code depends strictly on NoDash then we recommend using a uniform
way of specifying func
as a single array value and forego
function-specific cx
/args
.
If the context set by array func
’s 1st member or, in absence of such,
by cx
is undefined
then returned function preserves the
caller-specified context (aka “partial” application without context
binding).
Defined in: main.js, lines 2340-2342 (3 lines) • Show code
bind: function (func) {
return bind(func, arguments, 1)
},
Splits value
into chunks, all except last being length
in size.
Types | Notes |
---|---|
array of arrays/objects/strings | Empty if length is not positive. |
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
string | ||
length | omitted = 1 | |
int |
from unordered
Attention: be wary about object value
– JavaScript objects are
unordered.
chunk() groups keys in arbitrary order.
Defined in: main.js, lines 1889-1904 (16 lines) • Show code
chunk: function (value, length) {
var entries = NoDash.entries(value)
var res = []
if (length == null || length >= 1) {
while (entries.length) {
res.push(entries.splice(0, length || 1))
}
}
if (!NoDash.isArrayLike(value)) {
return res.map(NoDash.fromEntries)
} else if (typeof value == 'string') {
return res.map(function (a) { return NoDash.pluck(a, 1).join('') })
} else {
return res.map(function (a) { return NoDash.pluck(a, 1) })
}
},
Returns a copy of value
without falsy members.
Types | Notes |
---|---|
array | |
object |
Name | Types | Notes |
---|---|---|
value | array | |
object |
See also filter(), reject(), partition().
from fo
Warning: isArrayLike object
is treated as array
:
_.forEach({a: 2, 0: {b: 3}, length: '1'}, v => log(v))
// logs 2 (key 'a'), {b: 3} (key '0') and '1' (key 'length')
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v))
// logs only {b: 3} (key '0')
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a') //=> []
Give forceObject as the last argument to treat value
as an object:
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v), _.forceObject)
// logs 'a', '0', 'length'
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a', _.forceObject) //=> {a: 2}
Defined in: main.js, lines 1667-1669 (3 lines) • Show code
compact: function (value, forceObject) {
return NoDash.filter(value, function (item) { return item }, forceObject)
},
Calls func
for every member of value
, counting the number of times
each result was returned.
Types | Notes |
---|---|
object |
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
func | Subject to bind(); receives member’s value, its key and the
entire value ; returns a scalar. |
Defined in: main.js, lines 1498-1507 (10 lines) • Show code
countBy: function (value, func, cx) {
func = bind(func, arguments, 2)
var res = {}
NoDash.forEach(value, function () {
var group = func.apply(undefined, arguments)
res[group] = res[group] || 0
res[group]++
}, cx)
return res
},
Returns a function that invokes func
after ms
after the last calling
attempt.
Name | Types | Notes |
---|---|---|
func | Subject to bind() | |
ms | int | |
immediate | If truthy, calls func immediately when called for the
first time and then never calls for the subsequent ms |
See also throttle().
from tcan
Call cancel()
on the returned function to stop currently pending
invocation, if any.
Defined in: main.js, lines 2166-2187 (22 lines) • Show code
debounce: function (func, ms, immediate) {
func = bind(func)
var timer
if (immediate) {
var res = function debounce_() {
if (!timer) {
timer = setTimeout(function () { timer = null }, ms)
return func.apply(this, arguments)
}
}
} else {
var res = function debounce_() {
var args = [this, arguments]
clearTimeout(timer)
timer = setTimeout(function () {
func.apply(args[0], args[1])
}, ms)
}
}
res.cancel = function () { timer = clearTimeout(timer) }
return res
},
Calls func
outside of the current call stack, giving it args
.
from dl
Name | Types | Notes |
---|---|---|
func | Subject to bind() | |
ms | int |
Types | Notes |
---|---|
result of setTimeout() |
Calls delay() (mdn:API/WindowOrWorkerGlobalScope/setTimeout) with 0 delay.
Don’t rely on defer() to update a DOM node’s visual properties because modern browsers batch such changes and may even ignore them. Use redraw() instead, or delay() with a large timeout (>= 20 ms, less preferable).
Defined in: main.js, lines 2039-2043 (5 lines) • Show code
defer: function () {
var args = ap.slice.call(arguments)
args.splice(1, 0, 0)
return NoDash.delay.apply(undefined, args)
},
Calls func
after a delay of ms
, giving it args
.
Name | Types | Notes |
---|---|---|
func | dl | Subject to bind() |
ms | int |
Types | Notes |
---|---|
result of setTimeout() |
ECMAScript equivalent: mdn:API/WindowOrWorkerGlobalScope/setTimeout. See also defer().
Defined in: main.js, lines 2025-2027 (3 lines) • Show code
delay: function (func, ms) {
return setTimeout(bind(func, arguments, 2, true), ms)
},
Returns members of the first argument that are not listed in any other argument.
Types | Notes |
---|---|
array | |
object |
Name | Types | Notes |
---|---|---|
values | array | Can be of different types. |
object |
See also without(), intersection().
Defined in: main.js, lines 1736-1743 (8 lines) • Show code
difference: function (value) {
var args = NoDash.sortBy(ap.slice.call(arguments, 1), NoDash.negate(NoDash.size, true))
return NoDash.filter(value, function (item) {
return !NoDash.some(args, function (a) {
return NoDash.includes(a, item)
})
})
},
Returns true
if value
has sub
ending at endIndex
(or length
).
from sw
Name | Types | Notes |
---|---|---|
value | array | |
string | ||
sub | Same type as value ; if empty, always returns true | |
endIndex | omitted = length | Negative set to 0 (will match only
for empty sub ). |
int |
ECMAScript equivalent: o:String/endsWith (not in IE). See also startsWith().
Defined in: main.js, lines 914-917 (4 lines) • Show code
endsWith: function (value, sub, endIndex) {
var i = arguments.length < 3 ? value.length : endIndex > 0 ? endIndex : 0
return NoDash.startsWith(value, sub, i - sub.length)
},
Returns an array of arrays with key-value pairs for each member of
value
.
Types | Notes |
---|---|
array | 0th members (keys) are always strings (object value ) or
numbers (other). |
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
string |
ECMAScript equivalent: o:Object/entries (not in IE). See also fromEntries().
from unordered
Attention: be wary about object value
– JavaScript objects are
unordered.
entries() returns object value
’s pairs in arbitrary order.
from fo
Warning: isArrayLike object
is treated as array
:
_.forEach({a: 2, 0: {b: 3}, length: '1'}, v => log(v))
// logs 2 (key 'a'), {b: 3} (key '0') and '1' (key 'length')
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v))
// logs only {b: 3} (key '0')
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a') //=> []
Give forceObject as the last argument to treat value
as an object:
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v), _.forceObject)
// logs 'a', '0', 'length'
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a', _.forceObject) //=> {a: 2}
Defined in: main.js, lines 735-748 (14 lines) • Show code
entries: function (value) {
return transform(value, arguments, 0,
function () {
return NoDash.map(value, function (item, key) {
return [key, item]
})
},
Object.entries || function () {
return Object.keys(value).map(function (key) {
return [key, value[key]]
})
}
)
},
Replaces & < " '
in value
with equivalent HTML entitites.
Types | Notes |
---|---|
array | |
object | |
string |
Name | Types | Notes |
---|---|---|
value | array/object members escaped recursively (on isArrayLike basis,
forceObject not supported) | |
string | ||
null/undefined as empty string | ||
other treat as string |
The >
is not escaped since it’s not special unless your markup is
already broken (e.g. not using quotes for attributes). See
https://mathiasbynens.be/notes/ambiguous-ampersands (thanks LoDash,
lo:escape()).
$('<p>').text(value).html()
.<script>
, <template>
and <style>
Do not use escape() to generate content that will be inserted into
these tags! Not only value
’s semantics will be different when parsed,
there is also no guarantee that value
doesn’t break out of its tag
(read, XSS).
Sadly, there is no universal rule for these three tags because escaping style depends on the parsing context (expression, string content, etc.) and even the document’s HTML version.
In HTML prior to 5, the SGML spec
(https://www.w3.org/TR/html4/types.html#type-cdata) tells that only
the sequence </
terminates script
and style
(HTML 4 has no
template
). But in HTML 5 for “legacy reasons” the rules are more
involved and the spec
(https://html.spec.whatwg.org/multipage/scripting.html#restrictions-for-contents-of-script-elements)
suggests to escape <!--
, <script
and </script
(both
case-insensitive). In this example (HTML 5) the last line actually does
not close the tag which continues until the next </script>
(do note
that JavaScript context is irrelevant to these tokens):
<script>
const example = 'Consider this string: <!-- <script>';
</script>
But it works if <
is replaced with \x3C
inside the string:
const example = 'Consider this string: \x3C!-- \x3Cscript>';
However, it does not work with expressions where string escape sequences
are taken literally. Such places may be rewritten, for example by
inserting an insignificant whitespace after <
:
if (x<!--y) { ... }
if ( player<script ) { ... }
if ( player<// comment...
if (x\x3C!--y) { ... } // wrong
if (x< !--y) { ... } // correct
if (!--y > x) { ... } // correct
All this equally applies to template
as it does to script
.
This means you cannot just take an arbitrary JavaScript snippet, escape
it without parsing and insert into a script
tag. But you can use this
to escape JSON expressions placed inside script
or template
(since in
valid JSON <
may only appear within strings and they support \x3C
):
function escapeJSON(value) {
return JSON.stringify(value).replace(/<(!--|\/?script)/ig, '\\x3C$1')
}
var html = '<template>'
html += 'var data = ' + escapeJSON(['<!--', '</template>'])
html += '</template>'
Many popular frameworks (Flask, Django, Rails, etc.) wrongly provide the
one-size-fits-all escape function which even if prevents XSS inevitably
results in a mangled value (at best) when parsed. For example, PHP’s
json_encode()
converts /
to \/
by default but it has no effect on
<
so the following string results in an unclosed script
tag, eating
</script>
and the rest of the document:
<script>var s = <?=json_encode('<!--<script>')?></script>
In HTML 5 style
, <!--
has no special meaning and it’s enough to
simply replace all </style>
(case-insensitive) with something like
<\/style>
(\x3C
is unsupported but \/
equals literal /
). I
cannot think of a valid CSS where </style>
can appear as part of an
expression, not a string or a comment so I assume this rule is
context-free.
function escapeStyle(value) {
return (value + '').replace(/<\/(style>)/ig, '<\\/$1')
}
var html = '<style>'
html += 'body { background: url(' + escapeStyle('</style>.png') + '); }')
html += '</style>'
Thankfully, this all does not concern HTML attributes – as long as they are quoted, escape() will ensure the content does not break out:
var html = '<a onclick="alert(\'' + _.escape('</a>') + '\'">'
//=> <a onclick="alert('</a>')">
// When clicked, alerts: </a>
The described scheme is only for encoding HTML. Different tags’ DOM interfaces act yet differently from that:
<script>var x='<<'</script>
<!-- innerHTML and textContent are: var x='<<' -->
<template>var x='<<'</template>
<!-- innerHTML is: var x='<<' -->
<!-- textContent is blank -->
<style>var x='<<'</style>
<!-- innerHTML and textContent are the same as of <script> -->
<div>var x='<<'</div>
<!-- innerHTML is: var x='<<' -->
<!-- textContent is: var x='<<' -->
Defined in: main.js, lines 1141-1151 (11 lines) • Show code
escape: function (value) {
if (value == null) {
return ''
} else if (typeof value == 'object') {
return NoDash.map(value, NoDash.escape)
} else {
var to = {'&': 'amp', '<': 'lt', '"': 'quot', "'": '#39'}
return (value + '')
.replace(/[&<"']/g, function (m) { return '&' + to[m] + ';' })
}
},
Returns a string with all chararcters special within RegExp
delimiters
prepended with a backslash.
Defined in: main.js, lines 1158-1161 (4 lines) • Show code
escapeRegExp: function (str) {
// '-' in case escaping for a [character class], e.g. in trim*().
return str.replace(/[-^$.*+?{}()[\]|\\]/g, '\\$&')
},
Returns true
if func
returned truthyness for every member of value
.
from fe
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
func | Receives member’s value, its key and the entire value |
ECMAScript equivalent: o:Array/every. See also some().
from fo
Warning: isArrayLike object
is treated as array
:
_.forEach({a: 2, 0: {b: 3}, length: '1'}, v => log(v))
// logs 2 (key 'a'), {b: 3} (key '0') and '1' (key 'length')
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v))
// logs only {b: 3} (key '0')
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a') //=> []
Give forceObject as the last argument to treat value
as an object:
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v), _.forceObject)
// logs 'a', '0', 'length'
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a', _.forceObject) //=> {a: 2}
Defined in: main.js, lines 418-422 (5 lines) • Show code
every: function (value, func, cx) {
return transform(value, arguments, 1, ap.every, function () {
return NoDash.findIndex(value, NoDash.negate(func), cx, fo) === undefined
})
},
Returns a copy of value
with values of members in the given range
changed to filler
.
Types | Notes |
---|---|
array | |
object |
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
filler | mixed | |
begin | int | Starting index. |
omitted = 0 | ||
end | int | Index of the member after last included in the result. |
omitted = length |
ECMAScript equivalent: o:Array/fill (not in IE). See also repeat(), object().
from slend
Attention: slice(), o:String/substring() and fill() accept end
as an index (exclusive!) while o:Array/splice() and
o:String/substr() – as a length. Also, fill()’s end
cannot be
negative.
from unordered
Attention: be wary about object value
– JavaScript objects are
unordered.
fill() affects arbitrary keys if being
or end
is used.
Defined in: main.js, lines 700-715 (16 lines) • Show code
fill: function (value, filler, begin, end) {
if (NoDash.isArrayLike(value) && ap.fill) {
return ap.slice.call(value).fill(filler, begin, end)
} else {
var isArray = NoDash.isArrayLike(value)
value = NoDash.entries(value)
begin = begin || 0
if (arguments.length < 4) { end = Infinity }
for (; begin < end && begin < value.length; begin++) {
value[begin][1] = filler
}
return isArray
? value.map(function (item) { return item[1] })
: NoDash.fromEntries(value)
}
},
Returns a copy of value
without members for which func
has returned
falsyness.
Types | Notes |
---|---|
array | |
object |
from fe
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
func | Receives member’s value, its key and the entire value |
ECMAScript equivalent: o:Array/filter. See also reject(), compact(), partition().
from fo
Warning: isArrayLike object
is treated as array
:
_.forEach({a: 2, 0: {b: 3}, length: '1'}, v => log(v))
// logs 2 (key 'a'), {b: 3} (key '0') and '1' (key 'length')
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v))
// logs only {b: 3} (key '0')
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a') //=> []
Give forceObject as the last argument to treat value
as an object:
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v), _.forceObject)
// logs 'a', '0', 'length'
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a', _.forceObject) //=> {a: 2}
Defined in: main.js, lines 396-407 (12 lines) • Show code
filter: function (value, func, cx) {
return transform(value, arguments, 1, ap.filter, function () {
var res = {}
Object.keys(value).forEach(function (key) {
var item = value[key]
if (func.call(cx, item, key, value)) {
res[key] = item
}
})
return res
})
},
Returns first member for which func
has returned truthyness.
Types | Notes |
---|---|
mixed | |
undefined if not found |
from fe
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
func | Receives member’s value, its key and the entire value |
ECMAScript equivalent: o:Array/find (not in IE). See also findIndex() that returns the key.
from unordered
Attention: be wary about object value
– JavaScript objects are
unordered.
find() returns an arbitrary match if there are multiple matching object members.
from fo
Warning: isArrayLike object
is treated as array
:
_.forEach({a: 2, 0: {b: 3}, length: '1'}, v => log(v))
// logs 2 (key 'a'), {b: 3} (key '0') and '1' (key 'length')
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v))
// logs only {b: 3} (key '0')
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a') //=> []
Give forceObject as the last argument to treat value
as an object:
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v), _.forceObject)
// logs 'a', '0', 'length'
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a', _.forceObject) //=> {a: 2}
Defined in: main.js, lines 378-383 (6 lines) • Show code
find: function (value, func, cx) {
var index = NoDash.findIndex.apply(undefined, arguments)
// -1 can't occur for object, and if -1 then it's an array and so will
// return undefined unless value has something at -1 index (unlikely).
if (index !== undefined) { return value[index] }
},
Returns key of the first member of value
for which func
has returned
truthyness.
Types | Notes |
---|---|
int for array value (-1 if not found) | |
scalar for object (undefined
if not found) |
from fe
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
func | Receives member’s value, its key and the entire value |
ECMAScript equivalent: o:Array/findIndex (not in IE). See also find() that returns the value.
from unordered
Attention: be wary about object value
– JavaScript objects are
unordered.
findIndex() returns a key, not index for object value
, and it
returns an arbitrary match if there are multiple matching object members.
from fo
Warning: isArrayLike object
is treated as array
:
_.forEach({a: 2, 0: {b: 3}, length: '1'}, v => log(v))
// logs 2 (key 'a'), {b: 3} (key '0') and '1' (key 'length')
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v))
// logs only {b: 3} (key '0')
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a') //=> []
Give forceObject as the last argument to treat value
as an object:
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v), _.forceObject)
// logs 'a', '0', 'length'
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a', _.forceObject) //=> {a: 2}
Defined in: main.js, lines 346-363 (18 lines) • Show code
findIndex: function (value, func, cx) {
function iterator(item, key) {
if (func.apply(cx, arguments)) {
res = key
return true
}
}
var res = -1
var args = [iterator, arguments[arguments.length - 1] /*forceObject*/]
transform(value, args, 0, ap.some, function () {
res = undefined
// findIndex() is used by some() so it cannot call the latter.
Object.keys(value).some(function (key) {
return iterator(value[key], key, value)
})
})
return res
},
Returns at most first length
members of value
.
Types | Notes |
---|---|
frmixed if length is omitted, undefined on empty value | |
type of value |
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
string | ||
length | omitted return the value of one member | |
int return a slice of
value |
from unordered
Attention: be wary about object value
– JavaScript objects are
unordered.
This function returns arbitrary members for object value
.
Defined in: main.js, lines 1269-1271 (3 lines) • Show code
first: function (value, length) {
return slice(value, 0, arguments.length > 1 ? length : 1, arguments.length <= 1)
},
"Unwraps" nested arrays or objects in value
.
Types | Notes |
---|---|
array with sparse slots removed | |
object with duplicate keys keeping arbitrary value |
Name | Types | Notes |
---|---|---|
value | array | Nested members of the same type are flattened: arrays in array (in original positions), objects in object. |
object | ||
depth | int | Number of nesting levels to flatten; use
Infinity to flatten all. |
omitted = 1 |
ECMAScript equivalent: o:Array/flat (not in IE).
from unordered
Attention: be wary about object value
– JavaScript objects are
unordered.
flat() keeps value of the arbitrary key of duplicate object keys (see last example).
Defined in: main.js, lines 633-664 (32 lines) • Show code
flat: function (value, depth) {
depth = depth || 1
if (!NoDash.isArrayLike(value)) {
value = NoDash.entries(value)
while (--depth >= 0) {
var changed = false
for (var i = value.length - 1; i >= 0; i--) {
var item = value[i][1]
if (item instanceof Object) {
changed = value.splice.apply(value, [i, 1].concat(NoDash.entries(item)))
}
}
if (!changed) {
break
}
}
return NoDash.fromEntries(value)
} else if (ap.flat) {
return ap.flat.call(value, depth)
} else {
while (--depth >= 0) {
value = ap.concat.apply([], value.filter(function () { return true }))
// It's deemed faster to iterate over specific depths (which is
// typically 1) even without changing value than checking every member
// for array-likeness.
if (depth == Infinity && !value.some(NoDash.isArrayLike)) {
break
}
}
return value
}
},
LoDash
Name | Types | Notes |
---|---|---|
value | array | |
object |
Defined in: main.js, lines 2378-2380 (3 lines) • Show code
flattenDeep: function (value) {
return NoDash.flat(value, Infinity)
},
Returns a copy of value
with keys being former values and values being
former keys.
Types | Notes |
---|---|
object |
Name | Types | Notes |
---|---|---|
value | object |
from a2o
Defined in: main.js, lines 1869-1872 (4 lines) • Show code
flip: function (value) {
var unzipped = NoDash.unzip(NoDash.entries(value))
return NoDash.object(unzipped[1], unzipped[0])
},
Calls func
for every own member of value
.
Types | Notes |
---|---|
undefined |
Name | Types | Notes |
---|---|---|
value | fearray | |
object | ||
func | Receives member’s value, its key and the entire value |
ECMAScript equivalent: o:Array/forEach. See also invoke().
foWarning: isArrayLike object
is treated as array
:
_.forEach({a: 2, 0: {b: 3}, length: '1'}, v => log(v))
// logs 2 (key 'a'), {b: 3} (key '0') and '1' (key 'length')
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v))
// logs only {b: 3} (key '0')
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a') //=> []
Give forceObject as the last argument to treat value
as an object:
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v), _.forceObject)
// logs 'a', '0', 'length'
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a', _.forceObject) //=> {a: 2}
Defined in: main.js, lines 255-261 (7 lines) • Show code
forEach: function (value, func, cx) {
return transform(value, arguments, 1, ap.forEach, function () {
Object.keys(value).forEach(function (key) {
func.call(cx, value[key], key, value)
})
})
},
Returns an object constructed from arrays of key-value pairs.
Types | Notes |
---|---|
object |
Name | Types | Notes |
---|---|---|
value | array | |
object |
See also entries().
from unordered
Attention: be wary about object value
– JavaScript objects are
unordered.
“Order” of returned object’s keys won’t match order of pairs in value
.
Defined in: main.js, lines 766-772 (7 lines) • Show code
fromEntries: function (value) {
var obj = {}
NoDash.forEach(value, function (item) {
obj[item[0]] = item[1]
})
return obj
},
Puts every member of value
under its group determined by func
.
Types | Notes |
---|---|
object | Members are arrays or objects depending on value ’s type. |
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
func | Subject to bind(); receives member’s value, its key and the
entire value ; returns a scalar (member’s group). |
See also indexBy().
Defined in: main.js, lines 1449-1462 (14 lines) • Show code
groupBy: function (value, func, cx) {
var isArray = NoDash.isArrayLike(value)
func = bind(func, arguments, 2)
var res = {}
NoDash.forEach(value, function (item, key) {
var group = func.apply(undefined, arguments)
if (isArray) {
;(res[group] = res[group] || []).push(item)
} else {
;(res[group] = res[group] || {})[key] = item
}
})
return res
},
Returns true
if value
has defined property
.
Name | Types | Notes |
---|---|---|
value | Any object type – Array , Function , etc. |
ECMAScript equivalent: o:Object/hasOwnProperty. See also allKeys(), values().
Defined in: main.js, lines 866-868 (3 lines) • Show code
has: function (value, property) {
return Object.prototype.hasOwnProperty.call(value, property)
},
Returns true
if value
contains member
.
Name | Types | Notes |
---|---|---|
value | inarray | |
object | ||
string | ||
fromIndex | omitted = 0 | If negative, counts from the end; for
string value this differs from ECMAScript where 0 is assumed
(o:String/indexOf). |
int |
ECMAScript equivalent: o:Array/includes (not in IE). See also indexOf().
from fo
Warning: isArrayLike object
is treated as array
:
_.forEach({a: 2, 0: {b: 3}, length: '1'}, v => log(v))
// logs 2 (key 'a'), {b: 3} (key '0') and '1' (key 'length')
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v))
// logs only {b: 3} (key '0')
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a') //=> []
Give forceObject as the last argument to treat value
as an object:
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v), _.forceObject)
// logs 'a', '0', 'length'
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a', _.forceObject) //=> {a: 2}
forceObject is ignored if given a string value
.
Defined in: main.js, lines 480-483 (4 lines) • Show code
includes: function (value, member, fromIndex) {
var index = NoDash.indexOf.apply(undefined, arguments)
return index !== -1 /* remember: -1 == '-1' */ && index !== undefined
},
Changes keys of value
members to ones determined by func
.
Types | Notes |
---|---|
array | |
object |
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
func | Subject to bind(); receives member’s value, its key and the
entire value ; returns a scalar (member’s new key); of duplicate keys
only the last occurrence is kept (arbitrary for object value ). |
See also groupBy().
Defined in: main.js, lines 1476-1483 (8 lines) • Show code
indexBy: function (value, func, cx) {
func = bind(func, arguments, 2)
var res = value.constructor()
NoDash.forEach(value, function (item) {
res[func.apply(undefined, arguments)] = item
})
return res
},
Returns key of the first member
appearance in value
(===
), or
-1
(array/string value
) or undefined
(object value
).
from in
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
string | ||
fromIndex | omitted = 0 | If negative, counts from the end; for
string value this differs from ECMAScript where 0 is assumed
(o:String/indexOf). |
int |
ECMAScript equivalent: o:Array/indexOf. See also includes(), lastIndexOf().
from unordered
Attention: be wary about object value
– JavaScript objects are
unordered.
indexOf() returns key of arbitrary member
’s occurrence and does not
support fromIndex
.
If member
exists, result is always a number (array/string value
) or
string (object value
). The '-1'
string indicates a found member
for object value
so use ===
to test indexOf()’s result if value
may be of the either type:
_.indexOf([], 0) == _.indexOf({'-1': 0}, 0) //=> true: -1 == '-1'
_.indexOf([], 0) === _.indexOf({'-1': 0}, 0) //=> false
from fo
Warning: isArrayLike object
is treated as array
:
_.forEach({a: 2, 0: {b: 3}, length: '1'}, v => log(v))
// logs 2 (key 'a'), {b: 3} (key '0') and '1' (key 'length')
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v))
// logs only {b: 3} (key '0')
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a') //=> []
Give forceObject as the last argument to treat value
as an object:
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v), _.forceObject)
// logs 'a', '0', 'length'
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a', _.forceObject) //=> {a: 2}
forceObject is ignored if given a string value
.
Defined in: main.js, lines 513-525 (13 lines) • Show code
indexOf: function (value, member, fromIndex) {
// ap.indexOf.call('foo') works but only on substrings of 1 character.
if (typeof value == 'string') {
fromIndex < 0 && (fromIndex += value.length)
return value.indexOf(member, fromIndex)
}
return transform(value, arguments, 1, ap.indexOf, function (member, fromIndex) {
if (fromIndex != null) {
throw new TypeError('indexOf() does not support fromIndex for object value.')
}
return NoDash.findIndex(value, function (m) { return m === member }, fo)
})
},
Returns all members of value
except for last length
.
Types | Notes |
---|---|
initype of value |
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
string | ||
length | omitted = 1 | |
int |
from unordered
Attention: be wary about object value
– JavaScript objects are
unordered.
Defined in: main.js, lines 1305-1308 (4 lines) • Show code
initial: function (value, length) {
var i = typeof value == 'string' && value.length
return slice(value, 0, i + (arguments.length > 1 ? -length : -1))
},
Returns only members present in all given arguments.
Types | Notes |
---|---|
array in order of first argument | |
object with keys of first argument |
Name | Types | Notes |
---|---|---|
values | array | Can be of different types. |
object |
See also difference(), union(), assign().
Defined in: main.js, lines 1718-1725 (8 lines) • Show code
intersection: function (value) {
var args = NoDash.sortBy(ap.slice.call(arguments, 1), NoDash.size)
return NoDash.filter(value, function (item) {
return NoDash.every(args, function (a) {
return NoDash.includes(a, item)
})
})
},
Treats every member of value
as an object and calls method
on it,
returning results of all such calls.
Types | Notes |
---|---|
array | |
object |
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
method | str | |
args | Arguments that method receives, none by default. |
See also forEach().
from fo
Warning: isArrayLike object
is treated as array
:
_.forEach({a: 2, 0: {b: 3}, length: '1'}, v => log(v))
// logs 2 (key 'a'), {b: 3} (key '0') and '1' (key 'length')
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v))
// logs only {b: 3} (key '0')
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a') //=> []
Give forceObject as the last argument to treat value
as an object:
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v), _.forceObject)
// logs 'a', '0', 'length'
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a', _.forceObject) //=> {a: 2}
Defined in: main.js, lines 1337-1344 (8 lines) • Show code
invoke: function (value, method) {
var args = arguments
return transform(value, args, 2, function () {
var funcArgs = arguments
var invoke_ = function (item) { return item[method].apply(item, funcArgs) }
return NoDash.map(value, invoke_, args.forceObject)
})
},
Returns true
if value
is the special Arguments
object available
inside functions.
Defined in: main.js, lines 1190-1192 (3 lines) • Show code
isArguments: function (value) {
return Object.prototype.toString.call(value) == '[object Arguments]'
},
Returns true
if value
is an array-like object.
isArrayLike() is using the same criteria as ECMAScript’s
o:Array/from, that is: a numeric length
property.
See also isArray, toArray(), size(), forceObject.
Defined in: main.js, lines 1181-1183 (3 lines) • Show code
isArrayLike: function (value) {
return value != null && typeof value.length == 'number'
},
Returns true
if value
is a native DOM Element
.
See mdn:API/Node/nodeType.
Defined in: main.js, lines 1216-1218 (3 lines) • Show code
isElement: function (value) {
return value && value.nodeType === 1
},
Returns true
if value
is falsy or has zero length or no keys (for
non-isArrayLike objects).
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
string | ||
falsy returns true |
Defined in: main.js, lines 1203-1205 (3 lines) • Show code
isEmpty: function (value) {
return !value || (NoDash.isArrayLike(value) ? value : Object.keys(value)).length < 1
},
Determines if two values of arbitrary types are “equal”.
Types | Notes |
---|---|
bool |
Exists for compatibility with Underscore un:isEqual() and LoDash
lo:isEqual() that have them very elaborate. For example, two
different Date
objects may be “equal” if they reference exactly the
same timestamp.
NoDash’s isEqual() simply defers the task to JavaScript’s ==
operator
which is sufficient in most cases. Just keep in mind that if one argument
is a boolean or number, ==
converts the other to number:
'123' == 123 //=> true
true == 1 //=> true
NaN == NaN //=> false (!)
[] == false //=> true (!)
[] == 0 //=> true (!)
new Date > 0 //=> true
{} > 0 //=> true
{} == '[object Object]' //=> true (!)
null == undefined //=> true
Defined in: main.js, line 2374 • Show code
isEqual: function (a, b) { return a == b },
Returns a string consisting of stringified members of value
separated
by glue
.
Types | Notes |
---|---|
str |
Name | Types | Notes |
---|---|---|
value | array | null and undefined members are seen as blank
strings. |
object | ||
glue | str | |
undefined = , |
ECMAScript equivalent: o:Array/join.
from unordered
Attention: be wary about object value
– JavaScript objects are
unordered.
join() combines object value
’s members in arbitrary order.
from fo
Warning: isArrayLike object
is treated as array
:
_.forEach({a: 2, 0: {b: 3}, length: '1'}, v => log(v))
// logs 2 (key 'a'), {b: 3} (key '0') and '1' (key 'length')
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v))
// logs only {b: 3} (key '0')
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a') //=> []
Give forceObject as the last argument to treat value
as an object:
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v), _.forceObject)
// logs 'a', '0', 'length'
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a', _.forceObject) //=> {a: 2}
Defined in: main.js, lines 606-615 (10 lines) • Show code
join: function (value, glue) {
return transform(value, arguments, 1, ap.join, function (glue) {
return Object.keys(value)
.map(function (key) {
var item = value[key]
return item == null ? '' : item
})
.join(glue === undefined ? ',' : glue)
})
},
Returns keys of members in value
.
Types | Notes |
---|---|
array |
Name | Types | Notes |
---|---|---|
value | array | |
object |
ECMAScript equivalent: o:Object/keys. See also values().
from fo
Warning: isArrayLike object
is treated as array
:
_.forEach({a: 2, 0: {b: 3}, length: '1'}, v => log(v))
// logs 2 (key 'a'), {b: 3} (key '0') and '1' (key 'length')
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v))
// logs only {b: 3} (key '0')
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a') //=> []
Give forceObject as the last argument to treat value
as an object:
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v), _.forceObject)
// logs 'a', '0', 'length'
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a', _.forceObject) //=> {a: 2}
Defined in: main.js, lines 784-788 (5 lines) • Show code
keys: function (value) {
return transform(value, arguments, 0, function () {
return NoDash.range(value.length)
}, Object.keys)
},
Returns at most last length
members of value
.
from fr
Types | Notes |
---|---|
mixed if length is omitted, undefined on empty value | |
type of value |
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
string | ||
length | omitted return the value of one member | |
int return a slice of
value |
from unordered
Attention: be wary about object value
– JavaScript objects are
unordered.
This function returns arbitrary members for object value
.
Defined in: main.js, lines 1285-1287 (3 lines) • Show code
last: function (value, length) {
return slice(value, arguments.length > 1 ? -length : -1, undefined, arguments.length <= 1)
},
Returns key of the last member
appearance in value
(===
), or
-1
.
from in
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
string | ||
fromIndex | omitted = 0 | If negative, counts from the end; for
string value this differs from ECMAScript where 0 is assumed
(o:String/indexOf). |
int |
ECMAScript equivalent: o:Array/lastIndexOf. See also indexOf().
lastIndexOf() does not support object value
.
Defined in: main.js, lines 542-548 (7 lines) • Show code
lastIndexOf: function (value, member, fromIndex) {
if (typeof value == 'string') {
fromIndex < 0 && (fromIndex += value.length)
return value.lastIndexOf(member, fromIndex)
}
return transform(value, arguments, 1, ap.lastIndexOf, objectNotSupported('lastIndexOf'))
},
Returns a copy of value
with values replaced by results of calling
func
for each member.
Types | Notes |
---|---|
array | |
object |
from fe
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
func | Receives member’s value, its key and the entire value |
ECMAScript equivalent: o:Array/map. See also pluck().
from fo
Warning: isArrayLike object
is treated as array
:
_.forEach({a: 2, 0: {b: 3}, length: '1'}, v => log(v))
// logs 2 (key 'a'), {b: 3} (key '0') and '1' (key 'length')
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v))
// logs only {b: 3} (key '0')
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a') //=> []
Give forceObject as the last argument to treat value
as an object:
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v), _.forceObject)
// logs 'a', '0', 'length'
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a', _.forceObject) //=> {a: 2}
Defined in: main.js, lines 273-281 (9 lines) • Show code
map: function (value, func, cx) {
return transform(value, arguments, 1, ap.map, function () {
var res = {}
NoDash.forEach(value, function (v, k) {
res[k] = func.apply(cx, arguments)
}, fo)
return res
})
},
Returns the “maximum” member as ranked by func
.
Types | Notes |
---|---|
mixed | |
-Infinity if value is empty |
Name | Types | Notes |
---|---|---|
value | mxarray | |
object | ||
func | omitted take the member’s value as its rank | Subject to bind(); receives member’s value, its key and
the entire value |
function |
func
must produce numbers or cast-able strings. Without func
, this
condition applies to value
’s members.
ECMAScript equivalent: o:Math/max. See also min().
Defined in: main.js, lines 1377-1393 (17 lines) • Show code
max: function (value, func, cx) {
if (!func && NoDash.isArrayLike(value)) {
return Math.max.apply(undefined, value)
} else { // if func or (!func and non-array)
var max = -Infinity
var maxItem = -Infinity
func = bind(func, arguments, 2)
NoDash.forEach(value, function (item) {
var num = +(func ? func.apply(undefined, arguments) : item)
if (num > max) {
max = num
maxItem = item
}
})
return maxItem
}
},
Returns the “minimum” member as ranked by func
.
Types | Notes |
---|---|
mixed | |
+Infinity if value is empty |
from mx
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
func | omitted take the member’s value as its rank | Subject to bind(); receives member’s value, its key and
the entire value |
function |
func
must produce numbers or cast-able strings. Without func
, this
condition applies to value
’s members.
ECMAScript equivalent: o:Math/min. See also max().
Defined in: main.js, lines 1399-1405 (7 lines) • Show code
min: function (value, func, cx) {
func = bind(func, arguments, 2)
var res = NoDash.max(value, function (value) {
return -(func ? func.apply(undefined, arguments) : value)
})
return res == -Infinity ? Infinity : res
},
Returns a function that calls func
and inverts its result.
Name | Types | Notes |
---|---|---|
func | Subject to bind() | |
numeric | If truthy, changes sign of func ’s result, else treats it as
bool. |
Defined in: main.js, lines 1228-1234 (7 lines) • Show code
negate: function (func, numeric) {
func = bind(func)
return function negate_() {
var res = func.apply(this, arguments)
return numeric ? -res : !res
}
},
When NoDash is used outside of a module, restores old value of global _
and returns the NoDash
object.
Defined in: main.js, line 222
Returns an object constructed from given keys and values as separate lists.
Types | Notes |
---|---|
object |
Name | Types | Notes |
---|---|---|
keys | array | |
object | ||
values | same type as keys | Missing member for keys
assumes undefined , extra member is unused. |
omitted |
If keys
/values
are objects, their existing keys are ignored.
Defined in: main.js, lines 1850-1859 (10 lines) • Show code
object: function (keys, values) {
if (NoDash.isArrayLike(keys)) {
var zipped = NoDash.zip(keys, values || [])
} else {
values = values || {}
var zipped = Object.keys(NoDash.assign.apply(undefined, arguments))
.map(function (key) { return [keys[key], values[key]] })
}
return NoDash.fromEntries(zipped)
},
Returns own members of value
with mismatching keys.
from pk
function (value, func[, cx]) // matching each member by callback
function (value, keys) // keys given as an array
function (value, key[, key, ...]) // keys given directly as arguments
Types | Notes |
---|---|
array | |
object |
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
func | Given to filter(); this call form exists for compatibility with Underscore’s un:pick() where filter() cannot work on objects. |
from fo
Warning: isArrayLike object
is treated as array
:
_.forEach({a: 2, 0: {b: 3}, length: '1'}, v => log(v))
// logs 2 (key 'a'), {b: 3} (key '0') and '1' (key 'length')
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v))
// logs only {b: 3} (key '0')
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a') //=> []
Give forceObject as the last argument to treat value
as an object:
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v), _.forceObject)
// logs 'a', '0', 'length'
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a', _.forceObject) //=> {a: 2}
See also pick() that returns matching keys.
Defined in: main.js, lines 2008-2014 (7 lines) • Show code
omit: function (value, func, cx) {
var args = arguments
return transform(value, args, 1, function () {
func = pickerFunction(func, arguments)
return NoDash.reject(value, func, cx, args.forceObject)
})
},
Returns a function that invokes func
once, remembers its return value
and returns it for subsequent calls without invoking func
again.
Name | Types | Notes |
---|---|---|
func | Subject to bind(); if throws, future calls return undefined |
once() can be used for memoization, i.e. caching result of a heavy operation.
Defined in: main.js, lines 2198-2207 (10 lines) • Show code
once: function (func) {
var res = unset
return function once_() {
if (res === unset) {
res = undefined // in case func throws
res = bind(func).apply(this, arguments)
}
return res
}
},
Returns a copy of value
, with appended pad
if its length
was too
short.
from ps
Types | Notes |
---|---|
array | Always shallow-copied, even if value was long enough. |
string |
Name | Types | Notes |
---|---|---|
value | array | |
string | ||
length | int | Desired returned value’s length. |
pad | mixed for array value | |
string for string value | ||
omitted = undefined or ' ' |
ECMAScript equivalent: o:String/padEnd (not in IE). See also padStart().
Defined in: main.js, lines 963-966 (4 lines) • Show code
padEnd: function (value, length, pad) {
if (arguments.length < 3 && !Array.isArray(value)) { pad = ' ' }
return NoDash.padStart(value, length, pad, unset)
},
Returns a copy of value
, with prepended pad
if its length
was too
short.
Types | Notes |
---|---|
psarray | Always shallow-copied, even if value was long enough. |
string |
Name | Types | Notes |
---|---|---|
value | array | |
string | ||
length | int | Desired returned value’s length. |
pad | mixed for array value | |
string for string value | ||
omitted = undefined or ' ' |
ECMAScript equivalent: o:String/padStart (not in IE). See also padEnd().
Defined in: main.js, lines 938-948 (11 lines) • Show code
padStart: function (value, length, pad, end) {
if (!Array.isArray(value)) { value += '' } // Number, etc.
var add = Math.max(0, length - NoDash.size(value))
if (typeof value == 'string') {
if (arguments.length < 3) { pad = ' ' }
pad = Array(add + 1).join(pad).substr(0, add)
} else {
pad = NoDash.fill(Array(add), pad)
}
return end === unset ? value.concat(pad) : pad.concat(value)
},
Underscore/LoDash
Defined in: main.js, lines 2384-2387 (4 lines) • Show code
partial: function (func) {
var args = [func, undefined].concat(NoDash.rest(arguments))
return NoDash.bind.apply(undefined, args)
},
Splits members of value
into two groups determined by func
.
Types | Notes |
---|---|
array of matching, mismatching where each member is either an array
or object depending on value ’s type |
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
func | Subject to bind(); receives member’s value, its key and the
entire value ; result converted to bool. |
Defined in: main.js, lines 1644-1656 (13 lines) • Show code
partition: function (value, func, cx) {
func = bind(func, arguments, 2)
var isArray = NoDash.isArrayLike(value)
var mismatching = isArray ? [] : {}
var matching = NoDash.filter(value, function (item, key) {
if (func.apply(cx, arguments)) {
return true
} else {
isArray ? mismatching.push(item) : (mismatching[key] = item)
}
})
return [matching, mismatching]
},
Returns own members of value
with matching keys.
function (value, func[, cx]) // matching each member by callback
function (value, keys) // keys given as an array
function (value, key[, key, ...]) // keys given directly as arguments
Types | Notes |
---|---|
array | |
object |
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
func | Given to filter(); this call form exists for compatibility with Underscore’s un:pick() where filter() cannot work on objects. |
from fo
Warning: isArrayLike object
is treated as array
:
_.forEach({a: 2, 0: {b: 3}, length: '1'}, v => log(v))
// logs 2 (key 'a'), {b: 3} (key '0') and '1' (key 'length')
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v))
// logs only {b: 3} (key '0')
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a') //=> []
Give forceObject as the last argument to treat value
as an object:
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v), _.forceObject)
// logs 'a', '0', 'length'
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a', _.forceObject) //=> {a: 2}
See also omit() that returns mismatching keys, and pluck() that returns a single non-own property.
Do not be misled by the name: pick() iterates over value
rather than
reading keys
directly and will never return non-enumerable properties:
var obj = {enum: 1}
Object.defineProperty(obj, 'nonEnum', {value: 2, enumerable: false})
_.pick(obj, 'enum', 'nonEnum') //=> {enum: 1}
for (var k in obj) { console.log(k) } //=> 'enum' only
Defined in: main.js, lines 1990-1996 (7 lines) • Show code
pick: function (value, func, cx) {
var args = arguments
return transform(value, args, 1, function () {
func = pickerFunction(func, arguments)
return NoDash.filter(value, func, cx, args.forceObject)
})
},
Treats every member of value
as an object and collects their
property
.
Types | Notes |
---|---|
array | |
object |
Name | Types | Notes |
---|---|---|
value | array | |
object |
See also map(), pick(), unzip().
from fo
Warning: isArrayLike object
is treated as array
:
_.forEach({a: 2, 0: {b: 3}, length: '1'}, v => log(v))
// logs 2 (key 'a'), {b: 3} (key '0') and '1' (key 'length')
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v))
// logs only {b: 3} (key '0')
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a') //=> []
Give forceObject as the last argument to treat value
as an object:
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v), _.forceObject)
// logs 'a', '0', 'length'
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a', _.forceObject) //=> {a: 2}
Defined in: main.js, lines 1355-1358 (4 lines) • Show code
pluck: function (value, property) {
arguments[1] = function (obj) { return obj[property] }
return NoDash.map.apply(undefined, arguments)
},
Returns a function accepting an object and returning value of its property or of its sub-objects.
property() calls at() on the inside.
Defined in: main.js, lines 2217-2220 (4 lines) • Show code
property: function (path, def) {
if (arguments.length < 2) { def = unset }
return function property_(value) { return NoDash.at(value, path, def) }
},
Returns a random number.
Name | Types | Notes |
---|---|---|
() | Returns a float between 0 (inclusive) and 1 (exclusive). | |
max | Returns an int between 0 and max (inclusive) or max and 0 if
max is < 0. | |
min_max | Returns an int between min and max (inclusive). |
ECMAScript equivalent: o:Math/random. See also shuffle(), sample().
Replace this method to make other NoDash functions use another PRNG. Nice reference: https://stackoverflow.com/questions/521295/.
Defined in: main.js, lines 1574-1583 (10 lines) • Show code
random: function (min, max) {
switch (arguments.length) {
case 0:
return Math.random()
case 1:
min > 0 ? (max = min, min = 0) : (max = 0)
default:
return +min + Math.floor(NoDash.random() * (max - min + 1))
}
},
Returns an array of numbers, each different by step
.
Name | Types | Notes |
---|---|---|
end | If negative, returns a series from end (exclusive) to 0
(inclusive), else from 0 (inclusive) to end (exclusive). | |
begin_end | Returns a series from begin (inclusive) to end
(exclusive). | |
...step | int non-zero | Defaults to -1 or +1 depending on begin and
end |
See also times().
Defined in: main.js, lines 1922-1934 (13 lines) • Show code
range: function (begin, end, step) {
switch (arguments.length) {
case 1:
return begin > 0 ? NoDash.range(0, begin) : NoDash.range(begin, 1)
case 2:
step = begin > end ? -1 : +1
default:
return NoDash.fill(Array(Math.floor((end - begin) / step) || 0))
.map(function () {
return (begin += step) - step
})
}
},
Attempts to force redraw of the given DOM node
.
Name | Types | Notes |
---|---|---|
node | Element | |
class | omitted | |
str remove from classList and re-add (even if wasn’t
present) |
Types | Notes |
---|---|
node |
Use redraw() instead of defer() to update DOM state immediately
(dimensions, animation, etc.). For example, if .foo
class creates an
animation and you want to restart it (and don’t want to use
mdn:API/Animation), depending on your browser you may not see any
changes to node
if you simply remove and add that class – animation may
continue playing:
node.classList.remove('foo')
_.defer(function () { // = setTimeout(func, 0)
node.classList.add('foo')
})
Do this instead:
node.classList.remove('foo')
_.redraw(node)
node.classList.add('foo')
Or, the same:
_.redraw(node, 'foo')
Another example: because animation-delay
counts from the time the
animation has originally started playing (see
mdn:CSS/animation-delay#time), after changing it the animation will
usually be at some random frame unless restarted:
node.style.animationName = 'none'
_.redraw(node)
node.style.animationName = animation
node.style.animationDelay = -frame * interval + 'ms'
Note: redraw() doesn’t accept jQuery and other kinds of wrapped nodes:
_.redraw($('body')) // will not work
_.redraw($('body')[0]) // will work
Defined in: main.js, lines 2088-2093 (6 lines) • Show code
redraw: function (node, cls) {
cls == null || node.classList.remove(cls)
void node.offsetHeight
cls == null || node.classList.add(cls)
return node
},
Calls func
for every member of value
, returning result of the last
call.
Types | Notes |
---|---|
rdmixed as returned by func , or initial if given and value is empty |
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
func | Receives result of the previous func call, member’s value, its
key and the entire value | |
initial | omitted | If omitted, func is not called for the
first time; instead, that member’s value is used as if it was returned
by func ; will throw if omitted and value is empty. |
mixed |
ECMAScript equivalent: o:Array/reduce. See also sum() and
reduceRight() that iterates from the end of value
.
unorderedAttention: be wary about object value
– JavaScript objects are
unordered.
reduce() visits members of object value
in arbitrary order.
from fo
Warning: isArrayLike object
is treated as array
:
_.forEach({a: 2, 0: {b: 3}, length: '1'}, v => log(v))
// logs 2 (key 'a'), {b: 3} (key '0') and '1' (key 'length')
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v))
// logs only {b: 3} (key '0')
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a') //=> []
Give forceObject as the last argument to treat value
as an object:
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v), _.forceObject)
// logs 'a', '0', 'length'
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a', _.forceObject) //=> {a: 2}
Defined in: main.js, lines 310-317 (8 lines) • Show code
reduce: function (value, func, initial) {
return transform(value, arguments, 1, ap.reduce, function () {
arguments[0] = function (memo, item) {
return func(memo, item[1], item[0], value)
}
return ap.reduce.apply(NoDash.entries(value), arguments)
})
},
Calls func
for every member of value
starting from the last member,
returning result of the last call.
from rd
Types | Notes |
---|---|
mixed as returned by func , or initial if given and value is empty |
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
func | Receives result of the previous func call, member’s value, its
key and the entire value | |
initial | omitted | If omitted, func is not called for the
first time; instead, that member’s value is used as if it was returned
by func ; will throw if omitted and value is empty. |
mixed |
ECMAScript equivalent: o:Array/reduceRight. See also reduce() that
iterates from the start of value
.
reduceRight() does not support object value
.
Defined in: main.js, lines 327-329 (3 lines) • Show code
reduceRight: function (value, func, initial) {
return transform(value, arguments, 1, ap.reduceRight, objectNotSupported('reduceRight'))
},
Returns a copy of value
without members for which func
has returned
truthyness.
Types | Notes |
---|---|
array | |
object |
from fe
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
func | Receives member’s value, its key and the entire value |
See also filter(), compact(), partition().
Since reject() is non-standard, its func
is subject to bind() but
it’s better not to rely on this for symmetry and in case it gets
standardized.
Defined in: main.js, lines 1244-1246 (3 lines) • Show code
reject: function (value, func, cx) {
return NoDash.filter(value, NoDash.negate(func), cx)
},
Returns copies of value
duplicated count
times.
Types | Notes |
---|---|
array | |
string |
Name | Types | Notes |
---|---|---|
value | array | |
string |
ECMAScript equivalent: o:String/repeat (not in IE). See also fill().
Defined in: main.js, lines 977-980 (4 lines) • Show code
repeat: function (value, count) {
return typeof value == 'string' && value.repeat ? value.repeat(count)
: value.concat.apply(value.constructor(), NoDash.fill(Array(count), value))
},
Returns all members of value
except for first length
.
from ini
Types | Notes |
---|---|
type of value |
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
string | ||
length | omitted = 1 | |
int |
from unordered
Attention: be wary about object value
– JavaScript objects are
unordered.
Defined in: main.js, lines 1321-1323 (3 lines) • Show code
rest: function (value, length) {
return slice(value, arguments.length > 1 ? length : 1)
},
Returns the copy of values
with members in reverse order.
Types | Notes |
---|---|
array |
Name | Types | Notes |
---|---|---|
value | array |
ECMAScript equivalent: o:Array/reverse.
Defined in: main.js, lines 588-590 (3 lines) • Show code
reverse: function (value) {
return transform(value, [], 0, cloner(ap.reverse), objectNotSupported('reverse'))
},
Returns a random member of value
, or undefined
.
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
n | int | Exists for compatibility with Underscore’s un:sample() and LoDash’s lo:sample(); if given, sample() works just like shuffle() in NoDash. |
omitted |
See also random() and shuffle() that returns several random members
of value
.
Defined in: main.js, lines 1556-1562 (7 lines) • Show code
sample: function (value, n) {
if (arguments.length > 1) {
return NoDash.shuffle(v, n)
} else {
return NoDash.first(NoDash.shuffle(value, 1))
}
},
Returns a copy of value
with at most length
random members.
Types | Notes |
---|---|
array | |
object of random members of value |
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
length | omitted = length | |
int |
from unordered
Attention: be wary about object value
– JavaScript objects are
unordered.
If length
is ≥ value
’s size(), returned object is just a copy.
Defined in: main.js, lines 1524-1537 (14 lines) • Show code
shuffle: function (value, length) {
if (arguments.length < 2) { length = value.length }
var sized = NoDash.size(value)
var isArray = NoDash.isArrayLike(value)
value = isArray ? ap.slice.call(value) : NoDash.entries(value)
for (var i = 0; i < sized && i < length; i++) {
var j = NoDash.random(sized - 1)
var temp = value[i]
value[i] = value[j]
value[j] = temp
}
value.splice(length)
return isArray ? value : NoDash.fromEntries(value)
},
Returns number of members in value
, excluding “empty” slots in sparse
array.
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
string |
See also isArrayLike().
from fo
Warning: isArrayLike object
is treated as array
:
_.forEach({a: 2, 0: {b: 3}, length: '1'}, v => log(v))
// logs 2 (key 'a'), {b: 3} (key '0') and '1' (key 'length')
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v))
// logs only {b: 3} (key '0')
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a') //=> []
Give forceObject as the last argument to treat value
as an object:
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v), _.forceObject)
// logs 'a', '0', 'length'
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a', _.forceObject) //=> {a: 2}
Defined in: main.js, lines 1621-1630 (10 lines) • Show code
size: function (value, forceObject) {
if (forceObject !== fo && NoDash.isArrayLike(value)) {
return Array.isArray(value)
// reduce() relies on native forEach() to skip empty slots.
? NoDash.reduce(value, function (c) { return c + 1 }, 0)
: value.length // faster
} else {
return Object.keys(value).length
}
},
Returns a portion of value
.
Types | Notes |
---|---|
array | |
object |
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
begin | slbeint | Starting index. |
omitted = 0 | ||
end | int | Index of the member after last
included in the result; if negative set to length - end (hence give
-1 to omit last member). |
omitted = length |
ECMAScript equivalent: o:Array/slice.
slendAttention: slice(), o:String/substring() and fill() accept end
as an index (exclusive!) while o:Array/splice() and
o:String/substr() – as a length. Also, fill()’s end
cannot be
negative.
from unordered
Attention: be wary about object value
– JavaScript objects are
unordered.
slice() returns arbitrary members for object value
.
from fo
Warning: isArrayLike object
is treated as array
:
_.forEach({a: 2, 0: {b: 3}, length: '1'}, v => log(v))
// logs 2 (key 'a'), {b: 3} (key '0') and '1' (key 'length')
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v))
// logs only {b: 3} (key '0')
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a') //=> []
Give forceObject as the last argument to treat value
as an object:
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v), _.forceObject)
// logs 'a', '0', 'length'
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a', _.forceObject) //=> {a: 2}
Defined in: main.js, lines 575-579 (5 lines) • Show code
slice: function (value, begin, end) {
return transform(value, arguments, 1, ap.slice, function (begin, end) {
return NoDash.fromEntries(NoDash.entries(value, fo).slice(begin, end))
})
},
Returns true
if func
returned truthyness for any member of value
.
from fe
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
func | Receives member’s value, its key and the entire value |
ECMAScript equivalent: o:Array/some. See also every().
from fo
Warning: isArrayLike object
is treated as array
:
_.forEach({a: 2, 0: {b: 3}, length: '1'}, v => log(v))
// logs 2 (key 'a'), {b: 3} (key '0') and '1' (key 'length')
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v))
// logs only {b: 3} (key '0')
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a') //=> []
Give forceObject as the last argument to treat value
as an object:
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v), _.forceObject)
// logs 'a', '0', 'length'
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a', _.forceObject) //=> {a: 2}
Defined in: main.js, lines 433-437 (5 lines) • Show code
some: function (value, func, cx) {
return transform(value, arguments, 1, ap.some, function () {
return NoDash.findIndex(value, func, cx, fo) !== undefined
})
},
Returns a copy of value
with members sorted according to func
.
Types | Notes |
---|---|
array | Even for object value |
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
func | Receives av, bv, ak, bk and should return a positive value
if av must appear after bv , negative if before, or zero if they may
appear in any order (makes sorting unstable); ak and bk are their
keys within value and are only not given for object value |
ECMAScript equivalent: o:Array/sort. See also sortBy().
from fo
Warning: isArrayLike object
is treated as array
:
_.forEach({a: 2, 0: {b: 3}, length: '1'}, v => log(v))
// logs 2 (key 'a'), {b: 3} (key '0') and '1' (key 'length')
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v))
// logs only {b: 3} (key '0')
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a') //=> []
Give forceObject as the last argument to treat value
as an object:
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v), _.forceObject)
// logs 'a', '0', 'length'
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a', _.forceObject) //=> {a: 2}
Defined in: main.js, lines 451-459 (9 lines) • Show code
sort: function (value, func) {
return transform(value, arguments, 1, cloner(ap.sort), function () {
return NoDash.entries(value, fo)
.sort(function (a, b) {
return func.call(undefined, a[1], b[1], a[0], b[0])
})
.map(function (item) { return item[1] })
})
},
Returns a copy of value
with members sorted according to func
.
Types | Notes |
---|---|
array | Even for object value |
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
func | Subject to bind(); receives member’s value, its key and the
entire value ; returns a comparable (number or string); members with
identical ranks are sorted by their keys. |
See also sort().
Defined in: main.js, lines 1433-1436 (4 lines) • Show code
sortBy: function (value, func, cx) {
var entries = tagAndSort(value, func, cx)
return entries.map(function (item) { return item[1] })
},
Returns true
if value
has sub
at startIndex
(or 0).
Name | Types | Notes |
---|---|---|
value | swarray | |
string | ||
sub | Same type as value ; if empty, always returns true | |
startIndex | omitted = 0 | Negative set to 0. |
int |
ECMAScript equivalent: o:String/startsWith (not in IE). See also endsWith().
Defined in: main.js, lines 890-898 (9 lines) • Show code
startsWith: function (value, sub, startIndex) {
startIndex > 0 /*not undefined/NaN/etc.*/ || (startIndex = 0)
if (typeof value == 'string') {
return value.substr(startIndex, sub.length) == sub
}
var part = NoDash.slice(value, startIndex, startIndex + sub.length)
return part.length == sub.length &&
part.every(function (item, index) { return item === sub[index] })
},
Returns the sum of all members of value
.
Types | Notes |
---|---|
null if value is empty (conveniently, +null = 0 ) | |
int | |
NaN if any member couldn’t be cast to integer |
Name | Types | Notes |
---|---|---|
value | array | |
object |
See also reduce().
from fo
Warning: isArrayLike object
is treated as array
:
_.forEach({a: 2, 0: {b: 3}, length: '1'}, v => log(v))
// logs 2 (key 'a'), {b: 3} (key '0') and '1' (key 'length')
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v))
// logs only {b: 3} (key '0')
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a') //=> []
Give forceObject as the last argument to treat value
as an object:
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v), _.forceObject)
// logs 'a', '0', 'length'
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a', _.forceObject) //=> {a: 2}
Defined in: main.js, lines 1418-1421 (4 lines) • Show code
sum: function (value) {
ap.splice.call(arguments, 1, 0, function (m, n) { return m + +n }, null)
return NoDash.reduce.apply(undefined, arguments)
},
Returns a function that invokes func
not more often than once per ms
.
Name | Types | Notes |
---|---|---|
func | Subject to bind() | |
ms | int | |
options | object |
Possible options
keys:
Name | Types | Notes |
---|---|---|
leading | bool | |
omitted true | ||
trailing | bool | |
omitted true |
See also debounce().
tcanCall cancel()
on the returned function to stop currently pending
invocation, if any.
Consider this diagram (given ms
of 50, -
standing for 10 ms, >
for
time the returned function (RT) was first and last called):
c c c c func invocations
>L----TL----TL-->-T leading = trailing = true
^^^^^ ^^^^^ ^^^^^ throttle periods (50 ms)
>L---- L---- L--> leading = true, trailing = false
> ----T ----T -->-T leading = false, trailing = true
L
tells when func
is invoked if leading
is set, T
– if
trailing
is set.trailing
invokes func
after the end of the last period (even when RT
is no longer called).leading
and trailing
is useless.T
and L
in the beginning of the 2nd and
subsequent periods are “merged”, resulting in only one func
invocation.Defined in: main.js, lines 2127-2150 (24 lines) • Show code
throttle: function (func, ms, options) {
func = bind(func)
options = NoDash.assign({leading: true, trailing: true}, options)
var last = 0
var timer
function throttle_() {
var args = [this, arguments]
if (!timer) {
if (Date.now() - last >= ms && options.leading) {
func.apply(args[0], args[1])
last = Date.now()
}
timer = setTimeout(function () {
if (options.trailing) {
func.apply(args[0], args[1])
last = Date.now()
}
timer = null
}, ms)
}
}
throttle_.cancel = function () { timer = clearTimeout(timer) }
return throttle_
},
Calls func
several times
, returning results of all calls.
Types | Notes |
---|---|
array |
Name | Types | Notes |
---|---|---|
times | int | Non-negative. |
func | Subject to bind(); receives a number from 0 to times - 1 |
See also range().
Defined in: main.js, lines 2291-2296 (6 lines) • Show code
times: function (times, func, cx) {
func = bind(func, arguments, 2)
return NoDash.fill(Array(times)).map(function (aNull, index) {
return func.call(undefined, index)
})
},
Converts value
to a regular Array
.
Types | Notes |
---|---|
array |
Name | Types | Notes |
---|---|---|
value | array shallow-copied via slice() | |
object values returned | ||
other (null and typeof not object ) returned as [value] |
See also isArrayLike(), values().
Defined in: main.js, lines 1597-1606 (10 lines) • Show code
toArray: function (value) {
if (typeof value != 'object' || value == null) {
return [value]
} else if (!NoDash.isArrayLike(value)) {
return NoDash.values(value)
} else {
// Array.prototype.concat.call(arguments) returns [arguments].
return ap.slice.call(value)
}
},
Returns a copy of value
with certain “blanks” in start and end removed.
Types | Notes |
---|---|
trarray | |
string |
Name | Types | Notes |
---|---|---|
value | array | |
string | ||
blank | mixed for array value | |
string character list | ||
RegExp without /g flag | ||
omitted = falsy for array, whitespace (/\s/ ) for string |
ECMAScript equivalent: o:String/trim. See also trimStart(), trimEnd().
Defined in: main.js, lines 1000-1002 (3 lines) • Show code
trim: function (value, blank) {
return trim(true, true, value, blank, arguments.length < 2)
},
Returns a copy of value
with certain trailing “blanks” removed.
from tr
Types | Notes |
---|---|
array | |
string |
Name | Types | Notes |
---|---|---|
value | array | |
string | ||
blank | mixed for array value | |
string character list | ||
RegExp without /g flag | ||
omitted = falsy for array, whitespace (/\s/ ) for string |
ECMAScript equivalent: o:String/trimEnd (not in IE). See also trim(), trimStart().
Defined in: main.js, lines 1018-1020 (3 lines) • Show code
trimEnd: function (value, blank) {
return trim(false, true, value, blank, arguments.length < 2)
},
Returns a copy of value
with certain leading “blanks” removed.
from tr
Types | Notes |
---|---|
array | |
string |
Name | Types | Notes |
---|---|---|
value | array | |
string | ||
blank | mixed for array value | |
string character list | ||
RegExp without /g flag | ||
omitted = falsy for array, whitespace (/\s/ ) for string |
ECMAScript equivalent: o:String/trimStart (not in IE). See also trim(), trimEnd().
Defined in: main.js, lines 1009-1011 (3 lines) • Show code
trimStart: function (value, blank) {
return trim(true, false, value, blank, arguments.length < 2)
},
Returns a combination of members of all arguments without duplicates (unless they exist in the first argument).
Types | Notes |
---|---|
array | |
object |
Name | Types | Notes |
---|---|---|
values | array | |
object unlike with assign(), on duplicate key the first member’s value is kept |
ECMAScript equivalent: o:Array/concat. See also assign(), intersection().
Defined in: main.js, lines 1697-1708 (12 lines) • Show code
union: function () {
var res = arguments[0] || []
var isArray = NoDash.isArrayLike(res)
for (var i = 1; i < arguments.length; i++) {
NoDash.forEach(arguments[i], function (item, key) {
if (!NoDash.includes(res, item)) {
isArray ? res.push(item) : (res[key] = item)
}
})
}
return res
},
Returns a sorted copy of value
without identical (===
) members.
Types | Notes |
---|---|
array | |
object |
Name | Types | Notes |
---|---|---|
value | array | |
object | ||
func | function subject to bind(); ranking members of value as if by
groupBy() | |
omitted to sort by member’s string value |
from unordered
Attention: be wary about object value
– JavaScript objects are
unordered.
unique() keeps arbitrary key of identical object members (see last example).
Defined in: main.js, lines 1758-1776 (19 lines) • Show code
unique: function (value, func, cx) {
var prev = unset
function filterer(item) {
if (prev !== item) {
prev = item
return true
}
}
if (arguments.length < 2 && NoDash.isArrayLike(value)) {
return NoDash.sort(value).filter(filterer)
} else {
func = func || function (item) { return item + '' }
var entries = tagAndSort(value, func, cx)
.filter(function (item) { return filterer(item[2]) })
return NoDash.isArrayLike(value)
? entries.map(function (item) { return item[1] })
: NoDash.fromEntries(entries)
}
},
Splits members of value
into multiple collections, each containing all
members’ values for a particular property.
Types | Notes |
---|---|
array of arrays | |
object |
Name | Types | Notes |
---|---|---|
value | arrays of arrays | |
array of objects |
See also zip() that does the reverse, and pluck() that returns a single non-own property.
Defined in: main.js, lines 1823-1833 (11 lines) • Show code
unzip: function (value) {
if (NoDash.isArrayLike(value)) {
var list = NoDash.max(value, function (item) { return item.length })
} else {
var list = value.length
? NoDash.assign.apply(undefined, [{}].concat(value)) : [[]]
}
return NoDash.map(list, function (item, key) {
return NoDash.map(value, function (o) { return o[key] })
})
},
Returns values of members in value
.
Types | Notes |
---|---|
array with has() properties |
Name | Types | Notes |
---|---|---|
value | array | In all cases returns a shallow copy. |
object |
ECMAScript equivalent: o:Array/slice, o:Object/values (not in IE). See also toArray(), keys().
from fo
Warning: isArrayLike object
is treated as array
:
_.forEach({a: 2, 0: {b: 3}, length: '1'}, v => log(v))
// logs 2 (key 'a'), {b: 3} (key '0') and '1' (key 'length')
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v))
// logs only {b: 3} (key '0')
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a') //=> []
Give forceObject as the last argument to treat value
as an object:
_.forEach({a: 2, 0: {b: 3}, length: 1}, v => log(v), _.forceObject)
// logs 'a', '0', 'length'
_.pick({a: 2, 0: {b: 3}, length: 1}, 'a', _.forceObject) //=> {a: 2}
Defined in: main.js, lines 801-808 (8 lines) • Show code
values: function (value) {
// ap.slice() splits a string, ap.concat() doesn't.
return transform(value, arguments, 1, ap.slice, function () {
return Object.values
? Object.values(value)
: Object.keys(value).map(function (key) { return value[key] })
})
},
Returns a copy of values
without members
given as other arguments.
Types | Notes |
---|---|
array | |
object |
Name | Types | Notes |
---|---|---|
value | array | |
object |
See also difference().
Defined in: main.js, lines 1679-1684 (6 lines) • Show code
without: function (value) {
var values = ap.slice.call(arguments, 1)
return NoDash.filter(value, function (item) {
return values.indexOf(item) == -1
})
},
Puts member of every argument into that argument’s position in the unified returned collection.
Types | Notes |
---|---|
array of arrays | |
array of objects (of duplicate keys last is used) |
Name | Types | Notes |
---|---|---|
values | arrays of arrays | |
objects with array properties |
See also unzip() that does the reverse.
Defined in: main.js, lines 1797-1807 (11 lines) • Show code
zip: function () {
var args = ap.slice.call(arguments)
if (!NoDash.isArrayLike(args[0])) {
args = args.length
? NoDash.assign.apply(undefined, [{}].concat(args)) : [[]]
}
return NoDash.max(args, function (a) { return a.length })
.map(function (item, index) {
return NoDash.map(args, function (a) { return a[index] })
})
},