Skip to content

Commit

Permalink
rewrite splice to use toSpliced and add insertItems arg
Browse files Browse the repository at this point in the history
  • Loading branch information
DetachHead committed Jul 24, 2023
1 parent 1adea6b commit f64e515
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test and publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 19.7
node-version: 20.4
- run: npm ci --legacy-peer-deps
- run: npm run check
env:
Expand All @@ -26,7 +26,7 @@ jobs:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
with:
node-version: 19.7
node-version: 20.4
- run: echo ::set-output name=CURRENT_VERSION::$(node -p "require(\"./package.json\").version")
id: current-version
- if: github.ref == 'refs/heads/master'
Expand Down
11 changes: 9 additions & 2 deletions src/functions/Array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,19 @@ export const splice = <
const Array extends readonly unknown[],
StartIndex extends number,
DeleteCount extends number,
const InsertItems extends readonly unknown[],
>(
array: Array,
startIndex: StartIndex,
deleteCount: DeleteCount,
): Splice<Array, StartIndex, DeleteCount> =>
array.filter((_, index) => index < startIndex || index > deleteCount + 1) as never
...insertItems: InsertItems
): Splice<Array, StartIndex, DeleteCount, InsertItems> =>
array.toSpliced(startIndex, deleteCount, ...insertItems) as Splice<
Array,
StartIndex,
DeleteCount,
InsertItems
>

/**
* runs the given `predicate` on each value in the given `array`, and returns the index of the first value in the `array`
Expand Down
3 changes: 2 additions & 1 deletion src/types/Array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ export type Splice<
Array extends readonly unknown[],
StartIndex extends number,
DeleteCount extends number = Subtract<Array['length'], StartIndex>,
> = [...Take<Array, StartIndex>, ...Take<Array, Add<StartIndex, DeleteCount>, '<-'>]
InsertItems extends readonly unknown[] = [],
> = [...Take<Array, StartIndex>, ...InsertItems, ...Take<Array, Add<StartIndex, DeleteCount>, '<-'>]

/** removes the value at index `RemoveIndex` from `Array` */
export type RemoveIndex<
Expand Down
12 changes: 9 additions & 3 deletions test/functions/Array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,15 @@ describe('flat', () => {
})
})

test('splice', () => {
const expected = [1, 2, 6] as const
exactly(expected as Mutable<typeof expected>, splice([1, 2, 3, 4, 5, 6], 2, 3))
describe('splice', () => {
test('no inserted items', () => {
const expected = [1, 2, 6] as const
exactly(expected as Mutable<typeof expected>, splice([1, 2, 3, 4, 5, 6], 2, 3))
})
test('insert items', () => {
const expected = [1, 2, 'a', 'b', 6] as const
exactly(expected as Mutable<typeof expected>, splice([1, 2, 3, 4, 5, 6], 2, 3, 'a', 'b'))
})
})

describe('findIndexOfHighestNumber', () => {
Expand Down
7 changes: 4 additions & 3 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es2021" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
"target": "ESNext" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
"lib": ["ES2022"] /* Specify library files to be included in the compilation. */,
"lib": ["ESNext"] /* Specify library files to be included in the compilation. */,
"allowJs": false /* Allow javascript files to be compiled. */,
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
Expand Down Expand Up @@ -64,7 +64,8 @@
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */

/* Advanced Options */
"skipLibCheck": false /* Skip type checking of declaration files. */,
// https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/66122
"skipLibCheck": true /* Skip type checking of declaration files. */,
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */,

/* other options that weren't in the template */
Expand Down

0 comments on commit f64e515

Please sign in to comment.