Skip to content

Commit c985138

Browse files
ryan-williamsclaude
andcommitted
convert first lib utilities to TypeScript
Convert 6 leaf utility modules from `.js` to `.ts`: - `noop.ts` — empty function - `identity.ts` — generic identity function - `push_unique.ts` — array dedup helper - `mod.ts` — sanitized modulus functions - `is_plain_object.ts` — plain object type guard - `to_log_range.ts` — log scale conversion esbuild resolves `.ts` files transparently when `.js` is imported, so no import path changes needed. `tsc --noEmit` passes with zero errors. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 05f3a9c commit c985138

File tree

8 files changed

+17
-29
lines changed

8 files changed

+17
-29
lines changed

src/lib/identity.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/lib/identity.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default function identity<T>(d: T): T { return d; }

src/lib/is_plain_object.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/lib/is_plain_object.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export default function isPlainObject(obj: unknown): obj is Record<string, unknown> {
2+
if((globalThis as any).process?.versions) {
3+
return Object.prototype.toString.call(obj) === '[object Object]';
4+
}
5+
6+
return (
7+
Object.prototype.toString.call(obj) === '[object Object]' &&
8+
Object.getPrototypeOf(obj).hasOwnProperty('hasOwnProperty')
9+
);
10+
}

src/lib/mod.js renamed to src/lib/mod.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* sanitized modulus function that always returns in the range [0, d)
33
* rather than (-d, 0] if v is negative
44
*/
5-
function mod(v, d) {
5+
export function mod(v: number, d: number): number {
66
var out = v % d;
77
return out < 0 ? out + d : out;
88
}
@@ -11,15 +11,10 @@ function mod(v, d) {
1111
* sanitized modulus function that always returns in the range [-d/2, d/2]
1212
* rather than (-d, 0] if v is negative
1313
*/
14-
function modHalf(v, d) {
14+
export function modHalf(v: number, d: number): number {
1515
return Math.abs(v) > (d / 2) ?
1616
v - Math.round(v / d) * d :
1717
v;
1818
}
1919

20-
export { mod, modHalf };
21-
22-
export default {
23-
mod: mod,
24-
modHalf: modHalf
25-
};
20+
export default { mod, modHalf };
File renamed without changes.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
export default function pushUnique(array, item) {
1+
export default function pushUnique<T>(array: T[], item: T): T[] {
22
if(item instanceof RegExp) {
33
var itemStr = item.toString();
44
for(var i = 0; i < array.length; i++) {
5-
if(array[i] instanceof RegExp && array[i].toString() === itemStr) {
5+
if((array[i] as unknown) instanceof RegExp && (array[i] as unknown as RegExp).toString() === itemStr) {
66
return array;
77
}
88
}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import isNumeric from 'fast-isnumeric';
22

3-
export default function toLogRange(val, range) {
3+
export default function toLogRange(val: number, range: [number, number]): number {
44
if(val > 0) return Math.log(val) / Math.LN10;
55

6-
// move a negative value reference to a log axis - just put the
7-
// result at the lowest range value on the plot (or if the range also went negative,
8-
// one millionth of the top of the range)
96
var newVal = Math.log(Math.min(range[0], range[1])) / Math.LN10;
107
if(!isNumeric(newVal)) newVal = Math.log(Math.max(range[0], range[1])) / Math.LN10 - 6;
118
return newVal;

0 commit comments

Comments
 (0)