-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement @kitsuyui/symbolic-prototype
## Example ```ts import { SymbolicPrototype } from '@kitsuyui/symbolic-prototype'; const $ = SymbolicPrototype.NumberArray.scale; const array = [1, 2, 3]; const twice = array[$.scale](2); console.log(twice); // => [2, 4, 6] ``` ## Caution - I do not recommend using this package. It is just for fun. - This package is experimental. - This package is not intended to be used in production. - Breaking changes may be introduced in the future.
- Loading branch information
Showing
9 changed files
with
148 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# @kitsuyui/symbolic-prototype | ||
|
||
Using ES6 Symbol to avoid conflict with other libraries and native objects. | ||
In other words it is a modern version of prototype.js. | ||
|
||
## Example | ||
|
||
```ts | ||
import { SymbolicPrototype } from '@kitsuyui/symbolic-prototype'; | ||
const $ = SymbolicPrototype.NumberArray.scale; | ||
const array = [1, 2, 3]; | ||
const twice = array[$.scale](2); | ||
console.log(twice); // => [2, 4, 6] | ||
``` | ||
|
||
## Caution | ||
|
||
- I do not recommend using this package. It is just for fun. | ||
- This package is experimental. | ||
- This package is not intended to be used in production. | ||
- Breaking changes may be introduced in the future. | ||
|
||
## Install | ||
|
||
```sh | ||
npm install @kitsuyui/symbolic-prototype | ||
``` | ||
|
||
## License | ||
|
||
MIT |
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,23 @@ | ||
{ | ||
"name": "@kitsuyui/symbolic-prototype", | ||
"version": "0.0.0", | ||
"license": "MIT", | ||
"author": "Yui Kitsu <[email protected]>", | ||
"description": "Modern prototype extension with Symbol", | ||
"scripts": { | ||
"build": "tsup src/index.ts --clean", | ||
"dev": "pnpm build --watch" | ||
}, | ||
"exports": { | ||
".": { | ||
"import": "./dist/index.mjs", | ||
"require": "./dist/index.js", | ||
"types": "./dist/index.d.ts" | ||
} | ||
}, | ||
"main": "dist/index.js", | ||
"module": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"files": ["dist", "package.json"], | ||
"devDependencies": {} | ||
} |
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,37 @@ | ||
type BaseFunction<T, A extends unknown[]> = ( | ||
dummyThis: T, | ||
...args: A | ||
) => unknown | ||
|
||
/** | ||
* utility function to bind `this` as the first argument of a function | ||
* remaining arguments are passed as is to the original function | ||
* @param fn | ||
* @returns | ||
*/ | ||
export const bindThisAsFirstArgument = <T, A extends unknown[], R>( | ||
fn: (dummyThis: T, ...args: A) => R | ||
): ((this: T, ...args: A) => R) => { | ||
return function (this: T, ...args: A): R { | ||
return fn(this, ...args) | ||
} | ||
} | ||
|
||
/** | ||
* extend the prototype of a class with a new method | ||
* @param Target | ||
* @param symbol | ||
* @param fn | ||
*/ | ||
export const extend = <T, A extends unknown[], R>( | ||
Target: { prototype: T }, | ||
symbol: symbol, | ||
fn: BaseFunction<T, A> | ||
): void => { | ||
Object.defineProperty(Target.prototype, symbol, { | ||
value: bindThisAsFirstArgument(fn), | ||
writable: false, | ||
configurable: false, | ||
enumerable: false, | ||
}) | ||
} |
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,14 @@ | ||
import { describe, expect, it, jest } from '@jest/globals' | ||
|
||
import { SymbolicPrototype } from '.' | ||
|
||
describe('SymbolicPrototype', () => { | ||
it('NumberArray', () => { | ||
expect(SymbolicPrototype.NumberArray).toBeDefined() | ||
expect(SymbolicPrototype.NumberArray.scale).toBeDefined() | ||
const NA = SymbolicPrototype.NumberArray | ||
// example usage | ||
expect([1, 2, 3][NA.scale](2)).toStrictEqual([2, 4, 6]) | ||
expect([1, 2, 3][NA.sum]()).toBe(6) | ||
}) | ||
}) |
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,5 @@ | ||
import { Symbols } from './number-array' | ||
|
||
export const SymbolicPrototype = { | ||
NumberArray: Symbols, | ||
} as const |
11 changes: 11 additions & 0 deletions
11
packages/symbolic-prototype/src/number-array/index.spec.ts
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,11 @@ | ||
import { describe, expect, it, jest } from '@jest/globals' | ||
|
||
import { Symbols } from './index' | ||
|
||
describe('NumberArray', () => { | ||
it('scale', () => { | ||
const array: number[] = [1, 2, 3] | ||
const result = array[Symbols.scale](2) | ||
expect(result).toEqual([2, 4, 6]) | ||
}) | ||
}) |
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,24 @@ | ||
import { extend } from '../extend' | ||
|
||
export const Scale = Symbol('Scale') | ||
export const Sum = Symbol('Sum') | ||
|
||
export const Symbols = { | ||
scale: Scale, | ||
sum: Sum, | ||
} as const | ||
|
||
declare global { | ||
interface Array<T> { | ||
[Sum](this: Array<number>): number | ||
[Scale](this: Array<number>, scaler: number): number[] | ||
} | ||
} | ||
|
||
extend(Array, Scale, (array: number[], scaler: number) => { | ||
return array.map((value) => value * scaler) | ||
}) | ||
|
||
extend(Array, Sum, (array: number[]) => { | ||
return array.reduce((acc, value) => acc + value, 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