-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…precated 'Nameable' and 'TypeRef' related types, and added [pseudo] standalone (tsc) tests for 'Slice' type.
- Loading branch information
Showing
11 changed files
with
118 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
import {groupBy} from "./groupBy"; | ||
import {equal} from "../boolean"; | ||
import {Slice} from "../types"; | ||
|
||
/** | ||
* The group function takes a list and returns a list of lists such that | ||
* the concatenation of the result is equal to the argument. Moreover, each | ||
* sublist in the result contains only equal elements. For example: | ||
* | ||
* ```javascript | ||
* group("Mississippi".slice(0)) === [["M"], ["i"], ["s", "s"], ["i"], ["s", "s"], ["i"], ["p", "p"],[ "i"]] | ||
* group("Mississippi".slice(0)) === [["M"], ["i"], ["s", "s"], ["i"], ["s", "s"], ["i"], ["p", "p"], [ "i"]] | ||
* ``` | ||
*/ | ||
export const group = (xs: string | any[]): (string | any[])[] => groupBy(equal, xs); | ||
export const group = <T>(xs: Slice<T>): Slice<T>[] => groupBy(equal, xs); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
import {Slice} from "../types"; | ||
|
||
/** | ||
* Returns last item of string, and/or, array.. | ||
* @deprecated Use `at` instead. | ||
* | ||
* Returns last item of a slice. | ||
*/ | ||
export const last = (xs: string | any[]): string | any => xs.at(-1); | ||
export const last = <T = any, TS extends Slice<T> = Slice<T>>(xs: TS): T => xs.at(-1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import {Slice} from "../../types"; | ||
|
||
export const | ||
|
||
/** | ||
* Returns a copy of a slice (E.g., an array and/or a string). | ||
*/ | ||
sliceCopy = (xs: string | any[]): typeof xs => xs.slice(0) | ||
sliceCopy = (xs: Slice): typeof xs => xs.slice(0) | ||
|
||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* General ephemeral tests for `Slice` type - Tests just ensure that TSC (typescript compiler) | ||
* doesn't throw any errors when using the `Slice` type. | ||
*/ | ||
import {Slice} from "../../src"; | ||
|
||
const {stringify} = JSON; | ||
|
||
describe('Slice type', () => { | ||
it('Should be able to stand in for `string`, and `T[]` in function types', () => { | ||
const concat = <T extends Slice>(...xss: (T | ConcatArray<any>)[]): any => | ||
xss[0].slice(0, 0).concat(...xss); | ||
|
||
([ | ||
[['abc', 'def'], 'abcdef'], | ||
['abcdef'.split('').map(x => [x]), 'abcdef'] | ||
] as Slice[]) | ||
.forEach(([args, expected]) => { | ||
it(`function context test: concat(${args.map((x: any) => stringify(x)).join(', ')}) === ${stringify(expected)}`, () => { | ||
const result: string = concat(...args); // Ensure return value is inferred to our inline declarations type, | ||
// e.g., no TSC error is thrown | ||
|
||
expect(result).toEqual(expected); | ||
}); | ||
}); | ||
}); | ||
|
||
it('Should be able to stand in for `string`, and `T[]` types in standalone value declaration contexts;' + | ||
' e.g., it should not throw any TSC errors', () => { | ||
// Should be able to represent string values interchangeably (when calling `concat`, and/or `slice`, methods) | ||
const ctrlHead = 'all your base'; | ||
const ctrlTail = 'belong to us'; | ||
const someArray: Slice = ctrlHead.split(' ').map(x => x); | ||
|
||
expect(ctrlHead.concat(ctrlTail)).toEqual(ctrlHead + ctrlTail); | ||
|
||
expect(someArray.concat(...'all your base'.split(' ').map(xs => [xs]))).toEqual(['all', 'your', 'base']); | ||
|
||
// Should ignore generic type param when value is a string; E.g., should not throw error | ||
// ---- | ||
let someStr2: Slice<string> = 'hi'; | ||
|
||
someStr2 = ''.slice.call(someStr2, 0); | ||
expect(someStr2).toEqual('hi'); | ||
}); | ||
}); |