Skip to content

Commit bd6ad55

Browse files
committed
feat(utils): added basic string and array utils
1 parent 53d5342 commit bd6ad55

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+422
-3
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function filterArray<T, R extends T>(
2+
arrayToFilter: ReadonlyArray<T>,
3+
predicate: (value: T, index: number) => value is R
4+
): Array<R> {
5+
const result: R[] = []
6+
7+
for (let index = 0, length = arrayToFilter.length; index < length; index += 1) {
8+
const value = arrayToFilter[index]
9+
10+
if (predicate(value, index)) {
11+
result.push(value)
12+
}
13+
}
14+
15+
return result
16+
}
17+
18+
export { filterArray }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './filterArray'
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function filterArrayReverse<T, R extends T>(
2+
arrayToIterate: ReadonlyArray<T>,
3+
predicate: (value: T, index: number) => value is R
4+
): Array<R> {
5+
const result: R[] = []
6+
7+
for (let index = arrayToIterate.length; index--; ) {
8+
const value = arrayToIterate[index]
9+
10+
if (predicate(value, index)) {
11+
result.push(value)
12+
}
13+
}
14+
15+
return result
16+
}
17+
18+
export { filterArrayReverse }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './filterArrayReverse'
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function findInArray<T, R extends T>(
2+
arrayToIterate: ReadonlyArray<T>,
3+
predicate: (value: T, index: number) => value is R
4+
): R | undefined {
5+
let result: R | undefined
6+
7+
for (let index = 0, length = arrayToIterate.length; index < length; index += 1) {
8+
const value = arrayToIterate[index]
9+
10+
if (predicate(value, index)) {
11+
result = value
12+
break
13+
}
14+
}
15+
16+
return result
17+
}
18+
19+
export { findInArray }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './findInArray'
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function findInArrayReverse<T, R extends T>(
2+
arrayToIterate: ReadonlyArray<T>,
3+
predicate: (value: T, index: number) => value is R
4+
): R | undefined {
5+
let result: R | undefined
6+
7+
for (let index = arrayToIterate.length; index--; ) {
8+
const value = arrayToIterate[index]
9+
10+
if (predicate(value, index)) {
11+
result = value
12+
break
13+
}
14+
}
15+
16+
return result
17+
}
18+
19+
export { findInArrayReverse }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './findInArrayReverse'
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function forEachInArray<T>(arrayToIterate: ReadonlyArray<T>, callback: (value: T, index: number) => void): void {
2+
for (let index = 0, length = arrayToIterate.length; index < length; index += 1) {
3+
callback(arrayToIterate[index], index)
4+
}
5+
}
6+
7+
export { forEachInArray }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './forEachInArray'

0 commit comments

Comments
 (0)