Skip to content

Commit

Permalink
Workaround bug in deno_emit: cannot export mod
Browse files Browse the repository at this point in the history
  • Loading branch information
jflatow committed Sep 9, 2023
1 parent dfe649c commit 820e866
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
21 changes: 11 additions & 10 deletions sun.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,21 @@ function throttle(fun, every, T) {
async function timer(ms, val) {
return new Promise((okay)=>setTimeout(()=>okay(val), ms));
}
function __int(x) {
return parseInt(x, 10);
class Num {
static int(x) {
return parseInt(x, 10);
}
static mod(x, y) {
const r = x % y;
return r < 0 ? r + y : r;
}
}
function max(x, y) {
return x > y ? x : y;
}
function min(x, y) {
return x < y ? x : y;
}
function mod(x, y) {
const r = x % y;
return r < 0 ? r + y : r;
}
function nil(x) {
if (x instanceof Array) return [];
if (x instanceof Object) return {};
Expand Down Expand Up @@ -142,12 +144,12 @@ class Time extends Date {
const sep = opt?.sep ?? 'T', dsep = opt?.dsep ?? '-', tsep = opt?.tsep ?? ':';
const utc = opt?.utc || stamp[stamp.length - 1] == 'Z';
const dtp = stamp.split(sep);
const datep = dtp[0] ? dtp[0].split(dsep).map(__int) : [
const datep = dtp[0] ? dtp[0].split(dsep).map(Num.int) : [
0,
0,
0
];
const timep = dtp[1] ? dtp[1].substring(0, 8).split(':').map(__int) : [
const timep = dtp[1] ? dtp[1].substring(0, 8).split(':').map(Num.int) : [
0,
0,
0
Expand Down Expand Up @@ -203,10 +205,9 @@ class Time extends Date {
}
export { throttle as throttle };
export { timer as timer };
export { __int as int };
export { Num as Num };
export { max as max };
export { min as min };
export { mod as mod };
export { nil as nil };
export { pad as pad };
export { List as List };
Expand Down
6 changes: 4 additions & 2 deletions sun.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ Deno.test('throttle', async () => {
assertEquals(x, 12);
});

Deno.test('math', async () => {
assertEquals(Sun.int('016'), 16);
Deno.test('num, min, max', async () => {
assertEquals(Sun.Num.int('016'), 16);
assertEquals(Sun.Num.mod(25, 5), 0);
assertEquals(Sun.Num.mod(-25, 6), 5);
assertEquals(Sun.min(1, null), null);
assertEquals(Sun.max(1, null), 1);
assertEquals(Sun.min(null, 1), null);
Expand Down
22 changes: 12 additions & 10 deletions sun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@ export async function timer<V>(ms: number, val?: V): Promise<V | undefined> {
return new Promise((okay) => setTimeout(() => okay(val), ms));
}

export type Num = bigint | number;
export type Numeric = bigint | number;

export function int(x: string): number {
return parseInt(x, 10);
export class Num {
static int(x: string): number {
return parseInt(x, 10);
}

static mod<T extends Numeric>(x: T, y: T): T {
const r = x % y;
return r < 0 ? (r as any + y as any) : r;
}
}

export function max<T>(x?: T, y?: T): T | undefined {
Expand All @@ -27,11 +34,6 @@ export function min<T>(x?: T, y?: T): T | undefined {
return x! < y! ? x : y;
}

export function mod<T extends Num>(x: T, y: T): T {
const r = x % y;
return r < 0 ? (r as any + y as any) : r;
}

export function nil<T>(x?: T): T | undefined {
if (x instanceof Array)
return [] as T;
Expand Down Expand Up @@ -173,8 +175,8 @@ export class Time extends Date {
const sep = opt?.sep ?? 'T', dsep = opt?.dsep ?? '-', tsep = opt?.tsep ?? ':';
const utc = opt?.utc || stamp[stamp.length - 1] == 'Z';
const dtp = stamp.split(sep);
const datep = dtp[0] ? dtp[0].split(dsep).map(int) : [0, 0, 0];
const timep = dtp[1] ? dtp[1].substring(0, 8).split(':').map(int) : [0, 0, 0];
const datep = dtp[0] ? dtp[0].split(dsep).map(Num.int) : [0, 0, 0];
const timep = dtp[1] ? dtp[1].substring(0, 8).split(':').map(Num.int) : [0, 0, 0];
if (utc)
return new Date(Date.UTC(datep[0], datep[1] - 1, datep[2], timep[0], timep[1], timep[2]));
return new Date(datep[0], datep[1] - 1, datep[2], timep[0], timep[1], timep[2]);
Expand Down

0 comments on commit 820e866

Please sign in to comment.