@rohank/ts-utils 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -0
- package/dist/Mutable.d.ts +15 -0
- package/dist/Mutable.d.ts.map +1 -0
- package/dist/Mutable.js +3 -0
- package/dist/Unknown.d.ts +17 -0
- package/dist/Unknown.d.ts.map +1 -0
- package/dist/Unknown.js +3 -0
- package/dist/filterDistinct.d.ts +11 -0
- package/dist/filterDistinct.d.ts.map +1 -0
- package/dist/filterDistinct.js +26 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +27 -0
- package/dist/isNonEmptyArray.d.ts +8 -0
- package/dist/isNonEmptyArray.d.ts.map +1 -0
- package/dist/isNonEmptyArray.js +13 -0
- package/dist/isObject.d.ts +8 -0
- package/dist/isObject.d.ts.map +1 -0
- package/dist/isObject.js +12 -0
- package/dist/isPresent.d.ts +7 -0
- package/dist/isPresent.d.ts.map +1 -0
- package/dist/isPresent.js +12 -0
- package/package.json +53 -0
package/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# TS-Utils - TypeScript Utilities
|
|
2
|
+
**Author:** Rohan Khayech
|
|
3
|
+
|
|
4
|
+
TS-Utils provides a collection of utility functions and types to simplify common TypeScript patterns.
|
|
5
|
+
|
|
6
|
+
## Features
|
|
7
|
+
|
|
8
|
+
### Array Utilities
|
|
9
|
+
- **`filterDistinct<T>(array, key?):`** Filters an array to only include distinct elements.
|
|
10
|
+
|
|
11
|
+
### Type Guards
|
|
12
|
+
- **`isNonEmptyArray<T>(value):`** Checks if a value is a valid array with `length > 0`.
|
|
13
|
+
- **`isObject<T>(value):`** Returns whether a value is a defined, non-null object.
|
|
14
|
+
- **`isPresent<T>(value):`** Returns whether a value is defined and non-null.
|
|
15
|
+
|
|
16
|
+
### Type Utilities
|
|
17
|
+
- **`Mutable<T>:`** Makes all properties in type `T` mutable.
|
|
18
|
+
- **`Unknown<T>:`** Represents a valid, non-nullish object with properties of `T` as `unknown`.
|
|
19
|
+
|
|
20
|
+
## License and Copyright
|
|
21
|
+
Copyright © 2026 Rohan Khayech
|
|
22
|
+
|
|
23
|
+
TS-Utils is licensed under the [MIT license](LICENSE).
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility type that makes all properties in type `T` mutable.
|
|
3
|
+
*
|
|
4
|
+
* This should be only be used while building an object
|
|
5
|
+
* that should be treated as immutable once returned.
|
|
6
|
+
*
|
|
7
|
+
* This should not be used to modify an immutable type received
|
|
8
|
+
* externally, as often this will not have the intended effect.
|
|
9
|
+
*
|
|
10
|
+
* @author Rohan Khayech
|
|
11
|
+
*/
|
|
12
|
+
export type Mutable<T> = {
|
|
13
|
+
-readonly [P in keyof T]: T[P];
|
|
14
|
+
};
|
|
15
|
+
//# sourceMappingURL=Mutable.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Mutable.d.ts","sourceRoot":"","sources":["../src/Mutable.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;GAUG;AACH,MAAM,MAAM,OAAO,CAAC,CAAC,IAAI;IACrB,CAAC,UAAU,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CACjC,CAAA"}
|
package/dist/Mutable.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility type representing a valid, non-nullish object.
|
|
3
|
+
* This object may contain a partial set of the properties of `T` as `unknown` type.
|
|
4
|
+
*
|
|
5
|
+
* Used to model unchecked JSON data from an external source (eg. API responses, database documents).
|
|
6
|
+
* that may be of the correct format or may not.
|
|
7
|
+
*
|
|
8
|
+
* Allows typesafe querying of all potential properties, unlike `unknown` or `object`.
|
|
9
|
+
|
|
10
|
+
* @template T The expected object type.
|
|
11
|
+
*
|
|
12
|
+
* @author Rohan Khayech
|
|
13
|
+
*/
|
|
14
|
+
export type Unknown<T> = {
|
|
15
|
+
[K in keyof T]?: unknown;
|
|
16
|
+
};
|
|
17
|
+
//# sourceMappingURL=Unknown.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Unknown.d.ts","sourceRoot":"","sources":["../src/Unknown.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;GAYG;AACH,MAAM,MAAM,OAAO,CAAC,CAAC,IAAI;KACpB,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,EAAE,OAAO;CAC3B,CAAA"}
|
package/dist/Unknown.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Filters the specified array to only include elements with distinct keys.
|
|
3
|
+
*
|
|
4
|
+
* @param array The array to filter.
|
|
5
|
+
* @param key A function that returns a key given an element.
|
|
6
|
+
* @returns A copy of the specified array that only contains the first element with each unique key.
|
|
7
|
+
*
|
|
8
|
+
* @author Rohan Khayech
|
|
9
|
+
*/
|
|
10
|
+
export declare function filterDistinct<T>(array: T[], key?: (element: T) => any): T[];
|
|
11
|
+
//# sourceMappingURL=filterDistinct.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"filterDistinct.d.ts","sourceRoot":"","sources":["../src/filterDistinct.ts"],"names":[],"mappings":"AAEA;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAC5B,KAAK,EAAE,CAAC,EAAE,EACV,GAAG,GAAE,CAAC,OAAO,EAAE,CAAC,KAAK,GAA0B,GAChD,CAAC,EAAE,CAUL"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* Copyright (c) 2026 Rohan Khayech */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.filterDistinct = filterDistinct;
|
|
5
|
+
/**
|
|
6
|
+
* Filters the specified array to only include elements with distinct keys.
|
|
7
|
+
*
|
|
8
|
+
* @param array The array to filter.
|
|
9
|
+
* @param key A function that returns a key given an element.
|
|
10
|
+
* @returns A copy of the specified array that only contains the first element with each unique key.
|
|
11
|
+
*
|
|
12
|
+
* @author Rohan Khayech
|
|
13
|
+
*/
|
|
14
|
+
function filterDistinct(array, key) {
|
|
15
|
+
if (key === void 0) { key = function (element) { return element; }; }
|
|
16
|
+
var seen = new Set();
|
|
17
|
+
return array.filter(function (e) {
|
|
18
|
+
if (seen.has(key(e))) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
seen.add(key(e));
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions and types.
|
|
3
|
+
* @author Rohan Khayech
|
|
4
|
+
*/
|
|
5
|
+
export * from "./filterDistinct";
|
|
6
|
+
export * from "./isNonEmptyArray";
|
|
7
|
+
export * from "./isObject";
|
|
8
|
+
export * from "./isPresent";
|
|
9
|
+
export * from "./Mutable";
|
|
10
|
+
export * from "./Unknown";
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA;;;GAGG;AAEH,cAAc,kBAAkB,CAAA;AAChC,cAAc,mBAAmB,CAAA;AACjC,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA;AAC3B,cAAc,WAAW,CAAA;AACzB,cAAc,WAAW,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* Copyright (c) 2026 Rohan Khayech */
|
|
3
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
+
if (k2 === undefined) k2 = k;
|
|
5
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
8
|
+
}
|
|
9
|
+
Object.defineProperty(o, k2, desc);
|
|
10
|
+
}) : (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
o[k2] = m[k];
|
|
13
|
+
}));
|
|
14
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
15
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
16
|
+
};
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
/**
|
|
19
|
+
* Utility functions and types.
|
|
20
|
+
* @author Rohan Khayech
|
|
21
|
+
*/
|
|
22
|
+
__exportStar(require("./filterDistinct"), exports);
|
|
23
|
+
__exportStar(require("./isNonEmptyArray"), exports);
|
|
24
|
+
__exportStar(require("./isObject"), exports);
|
|
25
|
+
__exportStar(require("./isPresent"), exports);
|
|
26
|
+
__exportStar(require("./Mutable"), exports);
|
|
27
|
+
__exportStar(require("./Unknown"), exports);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if the given value is a valid, non empty array.
|
|
3
|
+
* @param value The value to check.
|
|
4
|
+
* @returns `true` if the value is a valid array (using `Array.isArray()`) with `length > 0`, `false` otherwise.
|
|
5
|
+
* @author Rohan Khayech
|
|
6
|
+
*/
|
|
7
|
+
export declare function isNonEmptyArray<T = unknown>(value: T[] | unknown): value is T[];
|
|
8
|
+
//# sourceMappingURL=isNonEmptyArray.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isNonEmptyArray.d.ts","sourceRoot":"","sources":["../src/isNonEmptyArray.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,CAAC,GAAG,OAAO,EACvC,KAAK,EAAE,CAAC,EAAE,GAAG,OAAO,GACrB,KAAK,IAAI,CAAC,EAAE,CAEd"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* Copyright (c) 2026 Rohan Khayech */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.isNonEmptyArray = isNonEmptyArray;
|
|
5
|
+
/**
|
|
6
|
+
* Checks if the given value is a valid, non empty array.
|
|
7
|
+
* @param value The value to check.
|
|
8
|
+
* @returns `true` if the value is a valid array (using `Array.isArray()`) with `length > 0`, `false` otherwise.
|
|
9
|
+
* @author Rohan Khayech
|
|
10
|
+
*/
|
|
11
|
+
function isNonEmptyArray(value) {
|
|
12
|
+
return Array.isArray(value) && value.length > 0;
|
|
13
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Unknown } from "./Unknown";
|
|
2
|
+
/**
|
|
3
|
+
* Returns whether the value is a defined, non-null object.
|
|
4
|
+
* @param value The value to check and return.
|
|
5
|
+
* @author Rohan Khayech
|
|
6
|
+
*/
|
|
7
|
+
export declare function isObject<T = Record<string, unknown>>(value: unknown): value is Unknown<T>;
|
|
8
|
+
//# sourceMappingURL=isObject.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isObject.d.ts","sourceRoot":"","sources":["../src/isObject.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAEnC;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAChD,KAAK,EAAE,OAAO,GACf,KAAK,IAAI,OAAO,CAAC,CAAC,CAAC,CAErB"}
|
package/dist/isObject.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* Copyright (c) 2026 Rohan Khayech */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.isObject = isObject;
|
|
5
|
+
/**
|
|
6
|
+
* Returns whether the value is a defined, non-null object.
|
|
7
|
+
* @param value The value to check and return.
|
|
8
|
+
* @author Rohan Khayech
|
|
9
|
+
*/
|
|
10
|
+
function isObject(value) {
|
|
11
|
+
return value !== null && typeof value === "object";
|
|
12
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isPresent.d.ts","sourceRoot":"","sources":["../src/isPresent.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,KAAK,IAAI,WAAW,CAAC,CAAC,CAAC,CAE9D"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* Copyright (c) 2026 Rohan Khayech */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.isPresent = isPresent;
|
|
5
|
+
/**
|
|
6
|
+
* Returns whether the value is defined and non-null.
|
|
7
|
+
* @param value The value to check and return.
|
|
8
|
+
* @author Rohan Khayech
|
|
9
|
+
*/
|
|
10
|
+
function isPresent(value) {
|
|
11
|
+
return value !== undefined && value !== null;
|
|
12
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rohank/ts-utils",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"files": [
|
|
5
|
+
"dist"
|
|
6
|
+
],
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"lint": "eslint --ext .js,.ts .",
|
|
10
|
+
"lint:fix": "eslint --ext .js,.ts . --fix",
|
|
11
|
+
"test": "jest --verbose",
|
|
12
|
+
"build": "tsc",
|
|
13
|
+
"clean": "rimraf dist"
|
|
14
|
+
},
|
|
15
|
+
"types": "dist/index.d.ts",
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@types/jest": "^30.0.0",
|
|
18
|
+
"@typescript-eslint/eslint-plugin": "^5.12.0",
|
|
19
|
+
"@typescript-eslint/parser": "^5.12.0",
|
|
20
|
+
"eslint": "^8.57.1",
|
|
21
|
+
"eslint-config-prettier": "^10.1.8",
|
|
22
|
+
"eslint-plugin-header": "^3.1.1",
|
|
23
|
+
"eslint-plugin-import": "^2.25.4",
|
|
24
|
+
"eslint-plugin-prettier": "^5.5.5",
|
|
25
|
+
"jest": "^30.2.0",
|
|
26
|
+
"prettier": "^3.8.1",
|
|
27
|
+
"rimraf": "^3.0.2",
|
|
28
|
+
"ts-jest": "^29.4.6",
|
|
29
|
+
"typescript": "^5.7.3"
|
|
30
|
+
},
|
|
31
|
+
"jest": {
|
|
32
|
+
"preset": "ts-jest",
|
|
33
|
+
"testEnvironment": "node",
|
|
34
|
+
"roots": [
|
|
35
|
+
"./test"
|
|
36
|
+
]
|
|
37
|
+
},
|
|
38
|
+
"description": "Collection of utility functions and types to simplify common TypeScript patterns.",
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "git+https://github.com/rohankhayech/TSUtils.git"
|
|
42
|
+
},
|
|
43
|
+
"keywords": [
|
|
44
|
+
"typescript",
|
|
45
|
+
"utilities"
|
|
46
|
+
],
|
|
47
|
+
"author": "Rohan Khayech",
|
|
48
|
+
"license": "MIT",
|
|
49
|
+
"bugs": {
|
|
50
|
+
"url": "https://github.com/rohankhayech/TSUtils/issues"
|
|
51
|
+
},
|
|
52
|
+
"homepage": "https://github.com/rohankhayech/TSUtils#readme"
|
|
53
|
+
}
|