Creates an array of elements split into groups the length of size. If array can't be split evenly, the final chunk will be the remaining elements.
array (Array): The array to process.
[size:=1] (number): The length of each chunk
(Array): Returns the new array of chunks.
_.chunk(["a", "b", "c", "d"], 2)
; => [["a", "b"], ["c", "d"]]
_.chunk(["a", "b", "c", "d"], 3)
; => [["a", "b", "c"], ["d"]]
Creates an array with all falsey values removed. The values false
, 0
, and ""
are falsey.
array (Array): The array to compact.
(Array): Returns the new array of filtered values.
_.compact([0, 1, false, 2, "", 3])
; => [1, 2, 3]
This method is explores the array and returns the maximum depth.
array (Array): The array to inspect.
(number): Returns the maximum depth.
_.depthOf([1])
; => 1
_.depthOf([1, [2]])
; => 2
_.depthOf([1, [[2]]])
; => 3
_.depthOf([1, [2, [3, [4]], 5]])
; => 4
Creates an array of array
values not included in the other given arrays. The order of result values are determined by the first array.
array (Array): The array to inspect.
values (Array*): The values to exclude.
(Array): Returns the new array of filtered values.
_.difference([2, 1], [2, 3])
; => [1]
Creates a slice of array
with n
elements dropped from the beginning.
array (Array): The array to query.
[n:=1] (number): The number of elements to drop.
(Array): Returns the slice of array.
_.drop([1, 2, 3])
; => [2, 3]
_.drop([1, 2, 3], 2)
; => [3]
_.drop([1, 2, 3], 5)
; => []
_.drop([1, 2, 3], 0)
; => [1, 2, 3]
_.drop("neo")
; => ["e", "o"]
_.drop(100)
; => ["0", "0"]
Creates a slice of array with n elements dropped from the end.
array (Array): The array to query.
[n:=1] (number): The number of elements to drop.
(Array): Returns the slice of array.
_.dropRight([1, 2, 3])
; => [1, 2]
_.dropRight([1, 2, 3], 2)
; => [1]
_.dropRight([1, 2, 3], 5)
; => []
_.dropRight([1, 2, 3], 0)
; => [1, 2, 3]
_.dropRight("neo")
; => ["n", "e"]
_.dropRight(100)
; => ["1", "0"]
Fills elements of array with value from start up to, but not including, end.
[!Note] This method mutates the array.
array (Array): The array to fill.
value (*): The value to fill array with.
[start:=1] (number): The start position.
[end:=array.length] (number): The end position.
(Array): Returns array.
arr := [1, 2, 3]
_.fill(arr, "a")
; => ["a", "a", "a"]
_.fill([4, 6, 8, 10], "*", 2, 3)
; => [4, "*", "*", 10]
Flattens array a single level deep.
array (Array): The array to flatten.
(Array): Returns the new flattened array.
_.flatten([1, [2, [3, [4]], 5]])
; => [1, 2, [3, [4]], 5]
_.flatten([[1, 2, 3], [4, 5, 6]])
; => [1, 2, 3, 4, 5, 6]
Recursively flattens array.
array (Array): The array to flatten.
(Array): Returns the new flattened array.
_.flattenDeep([1])
; => [1]
_.flattenDeep([1, [2]])
; => [1, 2]
_.flattenDeep([1, [2, [3, [4]], 5]])
; => [1, 2, 3, 4, 5]
Recursively flatten array up to depth times.
array (Array): The array to flatten.
[depth:=1] (number): The maximum recursion depth.
(Array): Returns the new flattened array.
_.flattenDepth([1, [2, [3, [4]], 5]], 1)
; => [1, 2, [3, [4]], 5]
_.flattenDepth([1, [2, [3, [4]], 5]], 2)
; => [1, 2, 3, [4], 5]
The inverse of _.toPairs; this method returns an object composed from key-value pairs.
pairs (Array): The key-value pairs.
(Object): Returns the new object.
_.fromPairs([["a", 1], ["b", 2]])
; => {a: 1, b: 2}
Gets the first element of array.
.first
array (Array): The array to query.
(*): Returns the first element of array.
_.head([1, 2, 3])
; => 1
_.head([])
; => ""
_.head("neo")
; => "n"
_.head(100)
; => "1"
Gets the index at which the first occurrence of value
is found in array
. If fromIndex
is negative, it's used as the offset from the end of array
.
array (Array): The array to inspect.
value (*): The value to search for.
[fromIndex:=1] (number): The index to search from.
(number): Returns the index of the matched value, else -1.
_.indexOf([1, 2, 1, 2], 2)
; => 2
; Search from the `fromIndex`.
_.indexOf([1, 2, 1, 2], 2, 3)
; => 4
_.indexOf(["neo", "morpheus"], "trinity")
; => -1
_.stringCaseSense := true
_.indexOf(["neo", "morpheus"], "Neo")
; => -1
Gets all but the last element of array.
array (Array): The array to query.
(Array): Returns the slice of array.
_.initial([1, 2, 3])
; => [1, 2]
_.initial("neo")
; => ["n", "e"]
_.initial(100)
; => ["1", "0"]
Creates an array of unique values that are included in all given arrays. The order of result values are determined by the first array.
[arrays*] (...Array): The arrays to inspect.
(Array): Returns the new array of intersecting values.
_.intersection([2, 1], [2, 3])
; => [2]
Converts all elements in array into a string separated by separator.
array (Array): The array to convert.
[separator:=","] (string): The element separator.
(string): Returns the joined string.
_.join(["a", "b", "c"], "~")
; => "a~b~c"
_.join(["a", "b", "c"])
; => "a,b,c"
Gets the last element of array.
array (Array): The array to query.
(*): Returns the last element of array.
_.last([1, 2, 3])
; => 3
_.last([])
; => ""
_.last("neo")
; => "o"
_.last(100)
; => 0
This method is like _.indexOf except that it iterates over elements of array from right to left.
array (Array): The array to inspect.
value (*): The value to search for.
[fromIndex:=array.length] (number): The index to search from.
(number): Returns the index of the matched value, else -1.
_.lastIndexOf([1, 2, 1, 2], 2)
; => 4
; Search from the `fromIndex`.
_.lastIndexOf([1, 2, 1, 2], 1, 3)
; => 3
_.stringCaseSense := true
_.lastIndexOf(["neo", "morpheus"], "Neo")
; => -1
Gets the element at index n of array. If n is negative, the nth element from the end is returned.
array (Array): The array to query.
[n:=1] (number): The index of the element to return.
(*): Returns the nth element of array.
_.nth([1, 2, 3])
; => 1
_.nth([1, 2, 3], -3)
; => 1
_.nth([1, 2, 3], 5)
; => ""
_.nth("neo")
; => "n"
_.nth(100)
; => "1"
_.nth([1, 2, 3], 0)
; => 1
Reverses array so that the first element becomes the last, the second element becomes the second to last, and so on.
array (Array): The array to modify.
(Array): Returns array.
_.reverse(["a", "b", "c"])
; => ["c", "b", "a"]
_.reverse([{foo: "bar"}, "b", "c"])
; => ["c", "b", {foo: "bar"}]
_.reverse([[1, 2, 3], "b", "c"])
; => ["c", "b", [1, 2, 3]]
Creates a slice of array from start up to end.
array (Array): The array to slice.
[start:=1] (number): The start position.
[end:=array.length] (number): The end position.
(Array): Returns the slice of array.
_.slice([1, 2, 3], 1, 2)
; => [1, 2]
_.slice([1, 2, 3], 1)
; => [1, 2, 3]
_.slice([1, 2, 3], 5)
; => []
_.slice("neo")
; => ["n", "e", "o"]
_.slice(100)
; => ["1", "0", "0"]
Uses a search to determine the lowest index at which value should be inserted into array in order to maintain its sort order.
array (Array): The sorted array to inspect.
value (*): The value to evaluate.
(number): Returns the index at which value should be inserted into array.
_.sortedIndex([30, 50], 40)
; => 2
_.sortedIndex([30, 50], 20)
; => 1
_.sortedIndex([30, 50], 99)
; => 3
This method is like _.uniq except that it's optimized for sorted arrays.
array (Array): The sorted array to inspect.
(array): Returns the new duplicate free array.
_.sortedUniq([1, 1, 2])
; => [1, 2]
Gets all but the first element of array.
array (Array): The array to query.
(Array): Returns the slice of array.
_.tail([1, 2, 3])
; => [2, 3]
_.tail("neo")
; => ["e", "o"]
_.tail(100)
; => ["0", "0"]
Creates a slice of array with n elements taken from the beginning.
array (Array): The array to query.
[n:=1] (number): The number of elements to take.
(Array): Returns the slice of array.
_.take([1, 2, 3])
; => [1]
_.take([1, 2, 3], 2)
; => [1, 2]
_.take([1, 2, 3], 5)
; => [1, 2, 3]
_.take([1, 2, 3], 0)
; => []
_.take("neo")
; => ["n"]
_.take(100)
; => ["1"]
Creates a slice of array with n elements taken from the end.
array (Array): The array to query.
[n:=1] (number): The number of elements to take.
(Array): Returns the slice of array.
_.takeRight([1, 2, 3])
; => [3]
_.takeRight([1, 2, 3], 2)
; => [2, 3]
_.takeRight([1, 2, 3], 5)
; => [1, 2, 3]
_.takeRight([1, 2, 3], 0)
; => []
_.takeRight("neo")
; => ["o"]
_.takeRight(100)
; => ["0"]
Creates an array of unique values, in order, from all given arrays.
[arrays] (...Array): The arrays to inspect.
(Array): Returns the new array of combined values.
_.union([2], [1, 2])
; => [2, 1]
Creates a duplicate-free version of an array, in which only the first occurrence of each element is kept. The order of result values is determined by the order they occur in the array.
array (Array): The array to inspect.
(Array): Returns the new duplicate free array.
_.uniq([2, 1, 2])
; => [2, 1]
This method is like _.zip except that it accepts an array of grouped elements and creates an array regrouping the elements to their pre-zip configuration.
array (Array): The array of grouped elements to process.
(Array): Returns the new array of regrouped elements.
zipped := _.zip(["a", "b"], [1, 2], [true, false])
; => [["a", 1, true], ["b", 2, false]]
_.unzip(zipped)
; => [["a", "b"], [1, 2], [true, false]]
Creates an array excluding all given values.
array (Array): The array to inspect.
[values] (...*): The values to exclude.
(Array): Returns the new array of filtered values.
_.without([2, 1, 2, 3], 1, 2)
; => [3]
Creates an array of grouped elements, the first of which contains the first elements of the given arrays, the second of which contains the second elements of the given arrays, and so on.
[arrays*] (...Array): The arrays to process.
(Array): Returns the new array of grouped elements.
_.zip(["a", "b"], [1, 2], [true, true])
; => [["a", 1, true], ["b", 2, true]]
This method is like _.fromPairs except that it accepts two arrays, one of property identifiers and one of corresponding values.
[props:=[]] (Array): The property identifiers.
[values:=[]] (Array): The property values.
(Object): Returns the new object.
_.zipObject(["a", "b"], [1, 2])
; => {a: 1, b: 2}
Checks if value is in collection. If collection is a string, it's checked for a substring of value.
collection (Array|Object|string): The collection to inspect.
value (*): The value to search for.
[fromIndex:=1] (number): The index to search from.
(boolean): Returns true if value is found, else false.
Creates an array of values by running each element in collection thru iteratee. The iteratee is invoked with three arguments: (value, index|key, collection).
Many adash.ahk methods are guarded to work as iteratees
The guarded methods are: trimEnd, trimStart, parseInt, chunk, take, takeRight, drop, sampleSize, words, random
collection (Array|Object): The collection to iterate over.
[iteratee:=.identity] (Function): The function invoked per iteration.
(Array): Returns the new mapped array.
fn_square(n) {
return n * n
}
_.map([4, 8], fn_square)
; => [16, 64]
_.map({ a: 4, b: 8 }, fn_square)
; => [16, 64]
_.map({ a: 4, b: 8 })
; => [4, 8]
Gets a single random element from collection
.
collection (Array|Object|String): The collection to sample.
(*): Returns the random element.
_.sample([1, 2, 3, 4])
; => 2
Creates an array of shuffled values, using a version of the Fisher-Yates shuffle.
collection (Array|Object): The collection to shuffle.
(Array): Returns the new shuffled array.
_.shuffle([1, 2, 3, 4])
; => [4, 1, 3, 2]
_.shuffle(["morpheus", "neo", "trinity"])
; => ["trinity", "morpheus", "neo"]
Gets the size of collection
by returning its length for array-like values or the number of own enumerable string keyed properties for objects.
collection (Array|Object|string): The collection to inspect.
(number): Returns the collection size.
_.size([1, 2, 3])
; => 3
_.size({ a: 1, b: 2 })
; => 2
_.size("trinity")
; => 7
Checks if predicate
returns truthy for any element of collection
. Iteration is stopped once predicate
returns truthy. The predicate is invoked with three arguments: (value, index|key, collection).
collection (Array|Object): The collection to iterate over.
[iteratees:=.identity] (Function): The function invoked per iteration.
(Array): Returns true if any element passes the predicate check, else false.
_.some(["foo", "bar", 42], _.isNumber)
; => true
Creates a shallow clone of value. Supports cloning arrays, objects, numbers, strings.
value (*): The value to clone.
(*): Returns the cloned value.
objects := [{ a: 1 }, { b: 2 }]
shallow := _.clone(objects)
_.isEqual(objects, shallow)
; => true
This method is like _.clone except that it recursively clones value.
value (*): The value to recursively clone.
(*): Returns the deep cloned value.
obj := [{ a: [[1, 2, 3]] }, { b: 2 }]
deepclone := _.cloneDeep(obj)
obj[1].a := 2
; object
; => [{ "a": 2 }, { "b": 2 }]
; deepclone
; => [{ "a": [[1, 2, 3]] }, { "b": 2 }]
Checks if value is an alnum.
value (*): The value to check.
(boolean): Returns true if value is an alnum, else false.
_.isAlnum(1)
; => false
_.isAlnum("hello")
; => true
_.isAlnum([])
; => false
_.isAlnum({})
; => false
Checks if value is an Array obj.
value (*): The value to check.
(boolean): Returns true if value is an array, else false.
_.isArray([1, 2, 3])
; => true
_.isArray("abc")
; => false
_.isArray({key: "value"})
; => false
Checks if value is classified as a boolean.
value (*): The value to check.
(boolean): Returns true if value is a boolean, else false.
_.isBoolean(true)
; => true
_.isBoolean(1)
; => true
_.isBoolean(false)
; => true
_.isBoolean(0)
; => true
Checks if value
is a buffer.
value (*): The value to check.
(boolean): Returns true if value is a buffer, else false.
myBuffer := buffer(20, 100)
_.isBuffer(myBuffer)
; => true
Checks if value
is an empty object, array, or map.
value (*): The value to check.
(boolean): Returns true
if value is empty, else `false.
_.isEmpty([])
; => true
_.isEmpty({})
; => true
_.isEmpty(map())
; => true
Performs a deep comparison between two values to determine if they are equivalent.
This method supports comparing strings and objects.
value (*): The value to compare.
other (...*): The other value to compare.
(boolean): Returns true if the values are equivalent, else false.
_.isEqual(1, 1)
; => true
_.isEqual({ a: 1 }, { a: 1 })
; => true
_.isEqual(1, 1, 2)
; => false
_.stringCaseSense := true
_.isEqual({ a: "a" }, { a: "A" })
; => false
Checks if value is a float.
value (*): The value to check.
(boolean): Returns true if value is a float, else false.
_.isFloat(1.0)
; => true
_.isFloat(1)
; => false
Checks if value
is callable as a function object, bound function, or object method.
value (*): The value to check.
(boolean): Returns true
if value
is callable, else false
.
bndFunc := strLen.bind("one")
_.isFunction(bndFunc)
; => true
_.isFunction(_.size)
; => true
_.isFunction([1, 2, 3])
; => false
Checks if value is an integer.
value (*): The value to check.
(boolean): Returns true if value is an integer, else false.
_.isInteger(1)
; => true
_.isInteger("1")
; => false
Checks if value
is classified as a Map object.
value (*): The value to check.
(boolean): Returns true
if value
is map, else false
.
_.isMap(map(1, "Neo"))
; => true
_.isMap([])
; => false
Performs a partial deep comparison between object
and source to determine if object
contains equivalent property values.
Partial comparisons will match empty array and empty object source
values against any array or object value, respectively. See _.isEqual.
object obj: The object to inspect.
source obj: The object of property values to match.
(boolean): Returns true if object is a match, else false.
obj := { a: 1, b: 2, c: 3 }
_.isMatch(obj, {b: 2})
; => true
_.isMatch(obj, {b: 2, c: 3})
; => true
_.isMatch(obj, {b: 1})
; => false
_.isMatch(obj, {b: 2, z: 99})
; => false
Checks if value is a number.
value (*): The value to check.
(boolean): Returns true if value is a number, else false.
_.isNumber(1)
; => true
_.isNumber("1")
; => true
_.isNumber("1.001")
; => true
Checks if value is an obj.
value (*): The value to check.
(boolean): Returns true if value is an object, else false.
_.isObject({})
; => true
_.isObject([1, 2, 3])
; => true
_.isObject("")
; => false
Checks if value is classified as a string.
value (*): The value to check.
(boolean): Returns true if value is a string, else false.
_.isString("abc")
; => true
_.isString(1)
; => false
Checks if value is undefined or blank.
value (*): The value to check.
(boolean): Returns true if value is undefined, else false.
_.isUndefined(non_existant_var)
; => true
_.isUndefined("")
; => false
_.isUndefined({})
; => false
_.isUndefined(" ")
; => false
_.isUndefined(0)
; => false
_.isUndefined(false)
; => false
Converts value to a string. An empty string is returned for undefined values. The sign of -0
is preserved.
value (*): The value to convert.
(boolean): Returns the converted string.
This method returns a string indicating the type of value
.
value (*): the value to check.
(string): Returns value's type.
_.typeOf(42)
; => "integer"
_.typeOf(0.25)
; => "float"
_.typeOf("0.25")
; => "string"
_.typeOf("blubber")
; => "string"
_.typeOf([])
; => "array"
_.typeOf({})
; => "object"
_.typeOf(map())
; => "map"
_.typeOf(object)
; => "class"
_.typeOf(_)
; => "class"
Adds two numbers.
augend (number): The first number in an addition.
addend (number): The second number in an addition.
(number): Returns the total.
_.add(6, 4)
; => 10
Computes number
rounded up to precision
.
number (number): The number to round up.
[precision:=0] (number): The precision to round up to.
(number): Returns the rounded up number.
_.ceil(4.006)
; => 5
_.ceil(6.004, 2)
; => 6.01
_.ceil(6040, -2)
; => 6100
Divide two numbers.
dividend (number): The first number in a division.
divisor (number): The second number in a division.
(number): Returns the quotient.
_.divide(6, 4)
; => 1.5
Computes number
rounded down to precision
.
number (number): The number to round down.
[precision:=0] (number): The precision to round down to.
(number): Returns the rounded down number.
_.floor(4.006)
; => 4
_.floor(0.046, 2)
; => 0.04
_.floor(4060, -2)
; => 4000
Computes the maximum value of array
. If array
is empty or falsey, ""
is returned.
array (Array): The array to iterate over.
(*): Returns the maximum value.
_.max([4, 2, 8, 6])
; => 8
_.max([])
; => ""
Computes the mean of the values in array
.
array (Array): The array to iterate over.
(number): Returns the mean.
_.mean([4, 2, 8, 6])
; => 5
Computes the minimum value of array
. If array is empty or falsey, ""
is returned.
array (Array): The array to iterate over.
(*): Returns the minimum value.
_.min([4, 2, 8, 6])
; => 2
_.min([])
; => ""
Multiply two numbers.
multiplier (number): The first number in a multiplication.
multiplicand (number): The second number in a multiplication.
(number): Returns the product.
_.multiply(6, 4)
; => 24
Computes number
rounded to precision
.
number (number): The number to round.
[precision:=0] (number): The precision to round to.
(number): Returns the rounded number.
_.round(4.006)
; => 4
_.round(4.006, 2)
; => 4.01
_.round(4060, -2)
; => 4100
Subtract two numbers.
minuend (number): The first number in a subtraction.
subtrahend (number): The second number in a subtraction.
(number): Returns the difference.
_.subtract(6, 4)
; => 2
Computes the sum of the values in array
.
array (Array): The array to iterate over.
(number): Returns the sum.
_.sum([4, 2, 8, 6])
; => 20
Clamps number within the inclusive lower and upper bounds.
number (number): The number to clamp.
lower (number): The lower bound.
upper (number): The upper bound.
(number): Returns the clamped number.
_.clamp(-10, -5, 5)
; => -5
_.clamp(10, -5, 5)
; => 5
Checks if n
is between start
and up to, but not including, end
. If end is not specified, it's set to start with start then set to 0. If start is greater than end the params are swapped to support negative ranges.
number (number): The number to check.
start (number): The start of the range.
end (number): The end of the range.
(boolean): Returns true if number is in the range, else false.
_.inRange(3, 2, 4)
; => true
_.inRange(4, 8)
; => true
_.inRange(4, 2)
; => false
_.inRange(2, 2)
; => false
_.inRange(1.2, 2)
; => true
_.inRange(5.2, 4)
; => false
_.inRange(-3, -2, -6)
; => true
Produces a random number between the inclusive lower and upper bounds. If floating is true, or either lower or upper are floats, a floating-point number is returned instead of an integer. Uses AutoHotkey's pseudo-random Random command.
[lower:=0] (number): The lower bound.
[upper:=1] (number): The upper bound.
[floating:=false] (boolean): Specify returning a floating-point number.
(number): Returns the random number.
_.random(0, 5)
; => an integer between 0 and 5
_.random(5)
; => an integer between 0 and 5
_.random(1.2, 5.2)
; => a floating-point number between 1.2 and 5.2
_.map(_.random([10, 10, 10]))
; => an array of random numbers between 0 and 10
Checks if string
ends with the given target string.
string (string): The string to inspect.
[target] (string): The string to search for.
[position:=strLen()] (number): The position to search up to.
(boolean): Returns true if string ends with target, else false.
_.endsWith("abc", "c")
; => true
_.endsWith("abc", "b")
; => false
_.endsWith("abc", "b", 2)
; => true
Converts the characters "&", "<", ">", '"', and "'" in string
to their corresponding HTML entities.
[!Note] No other characters are escaped. To escape additional characters use a third-party library.
Though the ">" character is escaped for symmetry, characters like ">" and "/" don't need escaping in HTML and have no special meaning unless they're part of a tag or unquoted attribute value. See Mathias Bynens's article (under "semi-related fun fact") for more details.
When working with HTML you should always quote attribute values to reduce XSS vectors.
[string:=""] (string): The string to escape.
(string): Returns the escaped string.
str := "neo, morpheus, & trinity"
_.escape(str)
; => "neo, morpheus, & trinity"
Converts the first character of string
to lower case.
[string:=""] (string): The string to convert.
(string): Returns the converted string.
_.lowerFirst("Neo")
; => "neo"
_.lowerFirst("FRED")
; => "fRED"
Pads string
on the left and right sides if it's shorter than length
. Padding characters are truncated if they can't be evenly divided by length.
[string:=""] (string): The string to pad.
[length:=0] (number): The padding length.
[chars:=" "] (string): The string used as padding.
(string): Returns the padded string.
_.pad("abc", 8)
; => " abc "
_.pad("abc", 8, "_-")
; => "_-abc_-_"
_.pad("abc", 3)
; => "abc"
Pads string
on the right side if it's shorter than length
. Padding characters are truncated if they exceed length.
[string:=""] (string): The string to pad.
[length:=0] (number): The padding length.
[chars:=" "] (string): The string used as padding.
(string): Returns the padded string.
_.padEnd("abc", 6)
; => "abc "
_.padEnd("abc", 6, "_-")
; => "abc_-_"
Pads string
on the left side if it's shorter than length
. Padding characters are truncated if they exceed length.
[string:=""] (string): The string to pad.
[length:=0] (number): The padding length.
[chars:=" "] (string): The string used as padding.
(string): Returns the padded string.
_.padStart("abc", 6)
; => " abc"
_.padStart("abc", 6, "_-")
; => "_-_abc"
Converts string to an integer of the specified radix. If radix is ""
undefined or 0
, a radix of 10
is used unless value is a hexadecimal, in which case a radix of 16
is used.
string (string): The string to convert.
[radix:=10] (number): The radix to interpret value by.
(number): Returns the converted integer.
_.parseInt("08")
; => 8
_.map(["6", "08", "10"], _.parseInt)
; => [6, 8, 10]
Repeats the given string n
times.
[string:=""] (string): The string to repeat.
[n:=1] (number): The number of times to repeat the string.
(string): Returns the repeated string.
_.repeat("*", 3)
; => "***"
_.repeat("abc", 2)
; => "abcabc"
_.repeat("abc", 0)
; => ""
Checks if string
starts with the given target string.
string (string): The string to inspect.
[target] (string): The string to search for.
[position:=1] (number): The position to search from.
(boolean): Returns true if string starts with target, else false.
_.startsWith("abc", "a")
; => true
_.startsWith("abc", "b")
; => false
_.startsWith("abc", "b", 2)
; => true
_.stringCaseSense := true
_.startsWith("abc", "A")
; => false
Converts string
, as a whole, to lower case.
string (string): The string to convert.
(string): Returns the lower cased string.
_.toLower("--Foo-Bar--")
; => "--foo-bar--"
_.toLower("fooBar")
; => "foobar"
_.toLower("__FOO_BAR__")
; => "__foo_bar__"
Converts string
, as a whole, to upper case
string (string): The string to convert.
(string): Returns the upper cased string.
_.toUpper("--foo-bar--")
; => "--FOO-BAR--"
_.toUpper("fooBar")
; => "FOOBAR"
_.toUpper("__foo_bar__")
; => "__FOO_BAR__"
Removes leading and trailing whitespace or specified characters from string
.
[string:=""] (string): The string to trim.
[chars:=whitespace] (string): The characters to trim.
(string): Returns the trimmed string.
_.trim(" abc ")
; => "abc"
_.trim("-_-abc-_-", "_-")
; => "abc"
_.map([" foo ", " bar "], _.trim)
; => ["foo", "bar"]
Removes trailing whitespace or specified characters from string
.
[string:=""] (string): The string to trim.
[chars:=whitespace] (string): The characters to trim.
(string): Returns the trimmed string.
_.trimEnd(" abc ")
; => " abc"
_.trimEnd("-_-abc-_-", "_-")
; => "-_-abc"
Removes leading whitespace or specified characters from string
.
[string:=""] (string): The string to trim.
[chars:=whitespace] (string): The characters to trim.
(string): Returns the trimmed string.
_.trimStart(" abc ")
; => "abc "
_.trimStart("-_-abc-_-", "_-")
; => "abc-_-"
Truncates string
if it's longer than the given maximum string length. The last characters of the truncated string are replaced with the omission string which defaults to "...".
[string:=""] (string): The string to truncate.
[options:={}] (Object): The options object.
[options.length:=30] (number): The maximum string length.
[options.omission:="..."] (string): The string to indicate text is omitted.
[options.separator] (RegExp|string): The separator pattern to truncate to.
(string): Returns the truncated string.
str := "hi-diddly-ho there, neighborino"
_.truncate(str)
; => "hi-diddly-ho there, neighbor..."
_.truncate(str, {length: 24, separator: " "})
; => "hi-diddly-ho there,..."
The inverse of _.escape this method converts the HTML entities &, <, >, ", and ' in string
to their corresponding characters.
[!Note] No other HTML entities are unescaped. To unescape additional HTML entities use a dedicated third-party library.
[string:=""] (string): The string to unescape.
(string): Returns the unescaped string.
str := "neo, morpheus, & trinity"
_.unescape(str)
; => "neo, morpheus, & trinity"
Converts the first character of string
to upper case.
[string:=""] (string): The string to convert.
(string): Returns the converted string.
_.upperFirst("neo")
; => "Neo"
_.upperFirst("FRED")
; => "FRED"
Splits string
into an array of its words.
string:="" (string): The string to inspect.
[pattern] (RegExp|string): The pattern to match words.
(Array): Returns the words of string.
_.words("neo, morpheus, & trinity")
; => ["neo", "morpheus", "trinity"]
_.words("neo, morpheus, & trinity", "[^, ]+")
; => ["neo", "morpheus", "&", "trinity"]
This method returns the first argument it receives.
value (*): Any value.
(*): Returns value.
obj := {a: 1}
_.identity(obj)
; => {a: 1}
assert.true(_.identity(obj) == obj)