ES5 + Lodash (Coderpad) Flashcards

(38 cards)

1
Q

Max value of a collection or object

A

_.max({ 5: ‘a’, 3: ‘b’ }, (value, key) => key)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Repeat a string X times

A
_.repeat('*', 3);
// → '***'

_.repeat(‘abc’, 2);
// → ‘abcabc’
~~~

_.repeat(‘abc’, 0);
// → ‘’
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Are any elements of this object or truthy?

A

_.some(collection)

The default value of the predicate parameter is _.identity.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Create the range [-1, 0, 1]

A

_.range(-1, 2)

Optional third param: step

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Remove an array element from an array.

A

_.reject(collection, arr => _.isEqual(arr, toRemove))

  • _.without() uses ES SameValueZero, which compares if objects are the exact same thing.*
  • _.remove mutates!*
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Check if n is between 0 and 4.

A

_.inRange(n, 0, 5)

Ranges seem to be exclusive.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Add an array to an array without mutation

A

[1, [0, 1]].concat([[0, 0]])

=> [1, [0, 1], [0, 0]]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Generate a new string with a char at index i replaced

A

str.slice(0, i) + newChar + str.slice(i + 1, str.length)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Add a list of elements to an array, return the array

A

List([1, 2]).push(1, 2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Class with a class method

A
class Dog {
 static bark() {}
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Class with a class variable

A
class Dog {}
Dog.type = 'awesome'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Range from 0 to n, inclusive

A

Range(0, n + 1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Range from 0 to n by 2, inclusive

A

Range(0, n + 1, 2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Range from n to 0, inclusive

A

Range(n, -1) // end is always exclusive

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Repeat value ‘a’ n times

A

Repeat(‘a’, 5)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Get from a hash, or default to 0

A

Map().get(‘a’, 0)

17
Q

Head/first, rest, initial, last?

A

Yup, all those are methods.

18
Q

Falsey values?

A

false 0 “” null undefined NaN

19
Q

Cast to int

A

Number.parseInt(‘13a’) // Forgiving: NaN only when first char isn’t number

=> 13

20
Q

Inspect class/instance methods, exclude inherited

A

Object.getOwnPropertyNames(Number)

Object.getOwnPropertyNames(Number.prototype)

21
Q

Inspect instance methods, include inherited

A

// Hard. Use Object.getOwnPropertyNames

22
Q

Switch statement

A
switch (obj) {
 case value: statement; break;
 case value: statement; break;
 default: statement; break;
 // Under the hood: obj === value
}
23
Q

Read file

A

fs.readFile(file, ‘utf8’, (err, file) => {})

24
Q

Read lines of file

A

fs.readFile(file, ‘utf8’, (err, file) => { file.split(“\n”) })

25
Trim whitespace from both sides of a string
" a ".trim()
26
Trim X from end of string
'xxabcxx'.replace(/x+$/, '') =\> 'xxabc'
27
Detect if beginning/end of string matches
'abc'.endsWith('c') // startsWith
28
Create a RegExp with interpolation
new RegExp(str, "gim") // global, ignore case, multiline
29
Detect if string matches a RegExp
/\w\s(\w+)/.test("Jason Benn") // RegExp method
30
Grab nth match group from string
/\w+\s(\w+)/.exec('Jason Benn')[1]
31
Find index of regex match
"Jason Benn".search(/\w\s(\w+)/) // Str method
32
Sort a map by a criteria
Map().sortBy((v, k, iter) =\> {})
33
Filter a map by a criteria
map.filter((v, k, iter) =\> {})
34
All elements of an array are truthy
[].every()
35
Any element of an array is truthy
[].some()
36
Initialize a date to a date object
new Date('...')
37
Display a date in a particular format
d.getHours(), d.getDate(), d.getMonth()+1, d.getFullYear()
38