-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImmArray.js
More file actions
38 lines (34 loc) · 880 Bytes
/
ImmArray.js
File metadata and controls
38 lines (34 loc) · 880 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import {freeze} from '../Util.js'
export const empty = freeze([])
/**
* @template T
* @param {number} count
* @param {T} value
* @returns {readonly T[]}
*/
export function repeat(count, value) {
return freeze(Array(count).fill(value))
}
/**
* @template T
* @param {readonly T[]} array
* @param {number} start
* @param {number} deleteCount
* @param {T[]} items
*/
export function spliced(array, start, deleteCount, ...items) {
// toSpliced() is not supported in target browsers
let mutArr = [...array]
mutArr.splice(start, deleteCount, ...items)
return freeze(mutArr)
}
/**
* @template T
* @param {readonly T[]} array
* @param {number} index
* @param {(item: T) => T} callback
*/
export function change(array, index, callback) {
// with() is not supported in target browsers
return spliced(array, index, 1, callback(array[index]))
}