@nimee/calculations 1.0.16 → 1.0.18
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/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/timeSlot.d.ts +24 -0
- package/dist/timeSlot.js +32 -0
- package/dist/timeSlot.js.map +1 -0
- package/package.json +3 -3
- package/src/index.ts +1 -0
- package/src/timeSlot.ts +42 -0
- package/tests/timeSlot.test.ts +79 -0
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -15,4 +15,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./prices"), exports);
|
|
18
|
+
__exportStar(require("./timeSlot"), exports);
|
|
18
19
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB;AACzB,6CAA2B"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
interface IDateOverride {
|
|
2
|
+
date: string;
|
|
3
|
+
schedule?: {
|
|
4
|
+
enabled?: boolean;
|
|
5
|
+
slotCapacity?: number;
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
interface IDaySchedule {
|
|
9
|
+
enabled?: boolean;
|
|
10
|
+
slotCapacity?: number;
|
|
11
|
+
}
|
|
12
|
+
export interface ITimeSlotConfig {
|
|
13
|
+
defaultCapacity?: number;
|
|
14
|
+
daySchedules?: Record<string, IDaySchedule>;
|
|
15
|
+
dateOverrides?: IDateOverride[];
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Resolves effective slot capacity for a given date.
|
|
19
|
+
* Priority: dateOverrides > daySchedules > defaultCapacity
|
|
20
|
+
*
|
|
21
|
+
* Returns 0 if the date is closed (override with null/disabled schedule).
|
|
22
|
+
*/
|
|
23
|
+
export declare function getEffectiveCapacity(timeSlotConfig: ITimeSlotConfig, date: string): number;
|
|
24
|
+
export {};
|
package/dist/timeSlot.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getEffectiveCapacity = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Resolves effective slot capacity for a given date.
|
|
6
|
+
* Priority: dateOverrides > daySchedules > defaultCapacity
|
|
7
|
+
*
|
|
8
|
+
* Returns 0 if the date is closed (override with null/disabled schedule).
|
|
9
|
+
*/
|
|
10
|
+
function getEffectiveCapacity(timeSlotConfig, date) {
|
|
11
|
+
var _a, _b;
|
|
12
|
+
if (!timeSlotConfig)
|
|
13
|
+
return 0;
|
|
14
|
+
if ((_a = timeSlotConfig.dateOverrides) === null || _a === void 0 ? void 0 : _a.length) {
|
|
15
|
+
const override = timeSlotConfig.dateOverrides.find((o) => o.date === date);
|
|
16
|
+
if (override) {
|
|
17
|
+
if (!override.schedule || !override.schedule.enabled)
|
|
18
|
+
return 0;
|
|
19
|
+
if (override.schedule.slotCapacity)
|
|
20
|
+
return override.schedule.slotCapacity;
|
|
21
|
+
return timeSlotConfig.defaultCapacity || 0;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const [year, month, day] = date.split("-").map(Number);
|
|
25
|
+
const dayOfWeek = new Date(year, month - 1, day).toLocaleDateString("en-US", { weekday: "long" }).toLowerCase();
|
|
26
|
+
const daySchedule = (_b = timeSlotConfig.daySchedules) === null || _b === void 0 ? void 0 : _b[dayOfWeek];
|
|
27
|
+
if ((daySchedule === null || daySchedule === void 0 ? void 0 : daySchedule.enabled) && (daySchedule === null || daySchedule === void 0 ? void 0 : daySchedule.slotCapacity))
|
|
28
|
+
return daySchedule.slotCapacity;
|
|
29
|
+
return timeSlotConfig.defaultCapacity || 0;
|
|
30
|
+
}
|
|
31
|
+
exports.getEffectiveCapacity = getEffectiveCapacity;
|
|
32
|
+
//# sourceMappingURL=timeSlot.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timeSlot.js","sourceRoot":"","sources":["../src/timeSlot.ts"],"names":[],"mappings":";;;AAgBA;;;;;GAKG;AACH,SAAgB,oBAAoB,CAAC,cAA+B,EAAE,IAAY;;IAChF,IAAI,CAAC,cAAc;QAAE,OAAO,CAAC,CAAC;IAE9B,IAAI,MAAA,cAAc,CAAC,aAAa,0CAAE,MAAM,EAAE,CAAC;QACzC,MAAM,QAAQ,GAAG,cAAc,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC;QAC3E,IAAI,QAAQ,EAAE,CAAC;YACb,IAAI,CAAC,QAAQ,CAAC,QAAQ,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,OAAO;gBAAE,OAAO,CAAC,CAAC;YAC/D,IAAI,QAAQ,CAAC,QAAQ,CAAC,YAAY;gBAAE,OAAO,QAAQ,CAAC,QAAQ,CAAC,YAAY,CAAC;YAC1E,OAAO,cAAc,CAAC,eAAe,IAAI,CAAC,CAAC;QAC7C,CAAC;IACH,CAAC;IAED,MAAM,CAAC,IAAI,EAAE,KAAK,EAAE,GAAG,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IACvD,MAAM,SAAS,GAAG,IAAI,IAAI,CAAC,IAAI,EAAE,KAAK,GAAG,CAAC,EAAE,GAAG,CAAC,CAAC,kBAAkB,CAAC,OAAO,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC;IAChH,MAAM,WAAW,GAAG,MAAA,cAAc,CAAC,YAAY,0CAAG,SAAS,CAAC,CAAC;IAE7D,IAAI,CAAA,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,OAAO,MAAI,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,YAAY,CAAA;QAAE,OAAO,WAAW,CAAC,YAAY,CAAC;IAEvF,OAAO,cAAc,CAAC,eAAe,IAAI,CAAC,CAAC;AAC7C,CAAC;AAnBD,oDAmBC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nimee/calculations",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.18",
|
|
4
4
|
"description": "nimi calculations like prices for back and front",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"author": "dan goldberg",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"test:deploy": "jest --ci --coverage"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
|
-
"@nimee/tests": "
|
|
21
|
+
"@nimee/tests": "0.0.38",
|
|
22
22
|
"@types/jest": "^23.3.12",
|
|
23
23
|
"@typescript-eslint/eslint-plugin": "^5.18.0",
|
|
24
24
|
"@typescript-eslint/eslint-plugin-tslint": "^5.18.0",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"types": "dist/index.d.ts",
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"@nimee/api-clients": "^1.1.15",
|
|
40
|
-
"@nimee/error-handler": "
|
|
40
|
+
"@nimee/error-handler": "0.0.14",
|
|
41
41
|
"@nimee/logger": "^1.0.27",
|
|
42
42
|
"axios": "1.2.1"
|
|
43
43
|
}
|
package/src/index.ts
CHANGED
package/src/timeSlot.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
interface IDateOverride {
|
|
2
|
+
date: string;
|
|
3
|
+
schedule?: { enabled?: boolean; slotCapacity?: number };
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
interface IDaySchedule {
|
|
7
|
+
enabled?: boolean;
|
|
8
|
+
slotCapacity?: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface ITimeSlotConfig {
|
|
12
|
+
defaultCapacity?: number;
|
|
13
|
+
daySchedules?: Record<string, IDaySchedule>;
|
|
14
|
+
dateOverrides?: IDateOverride[];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Resolves effective slot capacity for a given date.
|
|
19
|
+
* Priority: dateOverrides > daySchedules > defaultCapacity
|
|
20
|
+
*
|
|
21
|
+
* Returns 0 if the date is closed (override with null/disabled schedule).
|
|
22
|
+
*/
|
|
23
|
+
export function getEffectiveCapacity(timeSlotConfig: ITimeSlotConfig, date: string): number {
|
|
24
|
+
if (!timeSlotConfig) return 0;
|
|
25
|
+
|
|
26
|
+
if (timeSlotConfig.dateOverrides?.length) {
|
|
27
|
+
const override = timeSlotConfig.dateOverrides.find((o) => o.date === date);
|
|
28
|
+
if (override) {
|
|
29
|
+
if (!override.schedule || !override.schedule.enabled) return 0;
|
|
30
|
+
if (override.schedule.slotCapacity) return override.schedule.slotCapacity;
|
|
31
|
+
return timeSlotConfig.defaultCapacity || 0;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const [year, month, day] = date.split("-").map(Number);
|
|
36
|
+
const dayOfWeek = new Date(year, month - 1, day).toLocaleDateString("en-US", { weekday: "long" }).toLowerCase();
|
|
37
|
+
const daySchedule = timeSlotConfig.daySchedules?.[dayOfWeek];
|
|
38
|
+
|
|
39
|
+
if (daySchedule?.enabled && daySchedule?.slotCapacity) return daySchedule.slotCapacity;
|
|
40
|
+
|
|
41
|
+
return timeSlotConfig.defaultCapacity || 0;
|
|
42
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { getEffectiveCapacity, ITimeSlotConfig } from "../src/timeSlot";
|
|
2
|
+
|
|
3
|
+
const BASE_CONFIG: ITimeSlotConfig = {
|
|
4
|
+
defaultCapacity: 50,
|
|
5
|
+
daySchedules: {
|
|
6
|
+
wednesday: { enabled: true, slotCapacity: 20 },
|
|
7
|
+
thursday: { enabled: true, slotCapacity: 30 },
|
|
8
|
+
},
|
|
9
|
+
dateOverrides: [],
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
// 2026-04-01 is a Wednesday
|
|
13
|
+
const WEDNESDAY = "2026-04-01";
|
|
14
|
+
// 2026-04-02 is a Thursday
|
|
15
|
+
const THURSDAY = "2026-04-02";
|
|
16
|
+
// 2026-04-03 is a Friday (no day schedule)
|
|
17
|
+
const FRIDAY = "2026-04-03";
|
|
18
|
+
|
|
19
|
+
function cfg(overrides: Partial<ITimeSlotConfig>): ITimeSlotConfig {
|
|
20
|
+
return { ...BASE_CONFIG, ...overrides };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
describe("getEffectiveCapacity", () => {
|
|
24
|
+
it("returns 0 when timeSlotConfig is null/undefined", () => {
|
|
25
|
+
expect(getEffectiveCapacity(null as any, WEDNESDAY)).toBe(0);
|
|
26
|
+
expect(getEffectiveCapacity(undefined as any, WEDNESDAY)).toBe(0);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("resolves from dateOverrides when override exists with enabled schedule", () => {
|
|
30
|
+
const config = cfg({ dateOverrides: [{ date: WEDNESDAY, schedule: { enabled: true, slotCapacity: 10 } }] });
|
|
31
|
+
expect(getEffectiveCapacity(config, WEDNESDAY)).toBe(10);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("returns 0 when dateOverride explicitly disables the day (enabled: false)", () => {
|
|
35
|
+
const config = cfg({ dateOverrides: [{ date: WEDNESDAY, schedule: { enabled: false, slotCapacity: 20 } }] });
|
|
36
|
+
expect(getEffectiveCapacity(config, WEDNESDAY)).toBe(0);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("returns 0 when dateOverride has no schedule", () => {
|
|
40
|
+
const config = cfg({ dateOverrides: [{ date: WEDNESDAY }] });
|
|
41
|
+
expect(getEffectiveCapacity(config, WEDNESDAY)).toBe(0);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("falls back to defaultCapacity when override is enabled but has no slotCapacity", () => {
|
|
45
|
+
const config = cfg({ dateOverrides: [{ date: WEDNESDAY, schedule: { enabled: true } }] });
|
|
46
|
+
expect(getEffectiveCapacity(config, WEDNESDAY)).toBe(50);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("dateOverride takes priority over daySchedule", () => {
|
|
50
|
+
// Wednesday daySchedule has slotCapacity 20, but override wins
|
|
51
|
+
const config = cfg({ dateOverrides: [{ date: WEDNESDAY, schedule: { enabled: true, slotCapacity: 10 } }] });
|
|
52
|
+
expect(getEffectiveCapacity(config, WEDNESDAY)).toBe(10);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("resolves from daySchedules when no override matches the date", () => {
|
|
56
|
+
const config = cfg({ dateOverrides: [] });
|
|
57
|
+
expect(getEffectiveCapacity(config, WEDNESDAY)).toBe(20);
|
|
58
|
+
expect(getEffectiveCapacity(config, THURSDAY)).toBe(30);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("falls back to defaultCapacity when no override and no matching day schedule", () => {
|
|
62
|
+
const config = cfg({ dateOverrides: [] });
|
|
63
|
+
expect(getEffectiveCapacity(config, FRIDAY)).toBe(50);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("falls back to defaultCapacity when daySchedule exists but is disabled", () => {
|
|
67
|
+
const config = cfg({ daySchedules: { wednesday: { enabled: false, slotCapacity: 20 } }, dateOverrides: [] });
|
|
68
|
+
expect(getEffectiveCapacity(config, WEDNESDAY)).toBe(50);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("falls back to defaultCapacity when daySchedule is enabled but has no slotCapacity", () => {
|
|
72
|
+
const config = cfg({ daySchedules: { wednesday: { enabled: true } }, dateOverrides: [] });
|
|
73
|
+
expect(getEffectiveCapacity(config, WEDNESDAY)).toBe(50);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("returns 0 when no config fields are set", () => {
|
|
77
|
+
expect(getEffectiveCapacity({}, WEDNESDAY)).toBe(0);
|
|
78
|
+
});
|
|
79
|
+
});
|