Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
laffed committed Aug 31, 2022
0 parents commit f76b3e3
Show file tree
Hide file tree
Showing 11 changed files with 193 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
lib
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 John K

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @laffed/gadgets

Helpful utility types, aliases and methods
50 changes: 50 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "@laffed/gadgets",
"version": "1.0.0",
"description": "Typescript Gadgets",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"files": [
"lib/**/*"
],
"scripts": {
"build": "tsc -p tsconfig.json",
"prepare": "npm run build",
"version": "git add -A",
"postversion": "git push && git push --tags"
},
"repository": {
"type": "git",
"url": "git+ssh://[email protected]:laffed/gadgets.git"
},
"author": "laffed",
"license": "MIT",
"bugs": {
"url": "https://github.com/laffed/gadgets/issues"
},
"homepage": "https://github.com/laffed/gadgets/issues#readme",
"devDependencies": {
"@types/node": "^18.7.14",
"typescript": "^4.8.2"
}
}
1 change: 1 addition & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {}
1 change: 1 addition & 0 deletions src/enums/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {}
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './constants';
export * from './enums';
export * from './types';
export * from './methods';
5 changes: 5 additions & 0 deletions src/methods/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Entry } from '../types';

type Entries = <T extends object>(object: T) => ReadonlyArray<Entry<T>>;
export const entries: Entries = <T extends object>(obj: T) => Object.entries(obj) as unknown as ReadonlyArray<Entry<T>>;

43 changes: 43 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
export type ImmutablePrimitive = undefined | null | boolean | string | number | Function;

export type Immutable<T> =
T extends ImmutablePrimitive ? T :
T extends Array<infer U> ? ImmutableArray<U> :
T extends Map<infer K, infer V> ? ImmutableMap<K, V> :
T extends Set<infer M> ? ImmutableSet<M> : ImmutableObject<T>;

export type ImmutableArray<T> = ReadonlyArray<Immutable<T>>;
export type ImmutableMap<K, V> = ReadonlyMap<Immutable<K>, Immutable<V>>;
export type ImmutableSet<T> = ReadonlySet<Immutable<T>>;
export type ImmutableObject<T> = { readonly [K in keyof T]: Immutable<T[K]> };

export type OrNull<T> = T | null;

export type Flavoring<Flavor> = {
_type?: Flavor;
}
export type Flavor<T, Flavor> = T & Flavoring<Flavor>;

export type TupleEntry<T extends readonly unknown[], I extends unknown[] = [], R = never> =
T extends readonly [infer Head, ...infer Tail] ?
TupleEntry<Tail, [...I, unknown], R | [`${I['length']}`, Head]> :
R;

export type ObjectEntry<T> =
T extends object ?
{ [K in keyof T]: [K, Required<T>[K]] }[keyof T] extends infer E ?
E extends [infer K, infer V] ?
K extends string | number ?
[`${K}`, V] :
never :
never :
never :
never;

export type Entry<T extends object> =
T extends readonly [unknown, ...unknown[]] ?
TupleEntry<T> :
T extends ReadonlyArray<infer U> ?
[`${number}`, U] :
ObjectEntry<T>;

33 changes: 33 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"compilerOptions": {
"rootDir": "src",
"outDir": "./lib",
"target": "ESNext",
"types": [
"node",
],
"allowJs": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "CommonJS",
"moduleResolution": "node",
"isolatedModules": true,
"declarationMap": true,
"declaration": true
},
"include": [
"src"
],
"exclude": [
"node_modules",
],
"ts-node": {
"transpileOnly": true,
"files": true,
"compilerOptions": {
"rootDir": "."
}
}
}

0 comments on commit f76b3e3

Please sign in to comment.