@zajno/common 2.2.10 → 2.2.12
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.
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export interface IMonthIndex {
|
|
2
|
+
month: number;
|
|
3
|
+
}
|
|
4
|
+
export interface IWeekIndex extends IMonthIndex {
|
|
5
|
+
week: number;
|
|
6
|
+
}
|
|
7
|
+
export interface IDayIndex extends IWeekIndex {
|
|
8
|
+
day: number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Helps with converting sequential index to 0- or 1-based month, week, day index.
|
|
12
|
+
*
|
|
13
|
+
* Allows to use any number of days per week, not just 7. Also one can set any number of weeks per month (but it will be used globally).
|
|
14
|
+
*
|
|
15
|
+
* To update the value, either set `raw` property or use `set` method.
|
|
16
|
+
*
|
|
17
|
+
* Usage case: some ordered sequence of items, each has its own index, and you want to distribute them by months, weeks and days, knowing the number of items assigned per week.
|
|
18
|
+
*/
|
|
19
|
+
export declare class CalendarIndex implements IDayIndex {
|
|
20
|
+
readonly perWeek: number;
|
|
21
|
+
readonly base: 1 | 0;
|
|
22
|
+
static WEEKS_PER_MONTH: number;
|
|
23
|
+
/** sequential index */
|
|
24
|
+
raw: number;
|
|
25
|
+
/**
|
|
26
|
+
* @param perWeek how many indexes allocated per week
|
|
27
|
+
* @param [base=0] 1 or 0 - determines base for all indexes (0 by default)
|
|
28
|
+
*/
|
|
29
|
+
constructor(perWeek?: number, base?: 1 | 0);
|
|
30
|
+
get perMonth(): number;
|
|
31
|
+
/** 1/0-based month index based on `perWeek` property */
|
|
32
|
+
get month(): number;
|
|
33
|
+
get fullWeek(): number;
|
|
34
|
+
/** 1/0-based week index based on `perWeek` property */
|
|
35
|
+
get week(): number;
|
|
36
|
+
get day(): number;
|
|
37
|
+
set(month: number, week?: number, day?: number): this;
|
|
38
|
+
toIndex(): IDayIndex;
|
|
39
|
+
}
|
|
40
|
+
/** returns based (1/0) remainder of `value / div`
|
|
41
|
+
*
|
|
42
|
+
* Ex.: get day of week
|
|
43
|
+
*/
|
|
44
|
+
export declare function basedRemainder(this: void, base: 1 | 0, value: number, div: number): number;
|
|
45
|
+
/** returns based (1/0) floor'ed of `value / div` */
|
|
46
|
+
export declare function basedFloor(this: void, base: 1 | 0, value: number, div: number): number;
|
|
47
|
+
//# sourceMappingURL=calendarIndex.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"calendarIndex.d.ts","sourceRoot":"","sources":["../../src/dates/calendarIndex.ts"],"names":[],"mappings":"AACA,MAAM,WAAW,WAAW;IACxB,KAAK,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,UAAW,SAAQ,WAAW;IAC3C,IAAI,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,SAAU,SAAQ,UAAU;IACzC,GAAG,EAAE,MAAM,CAAC;CACf;AAED;;;;;;;;GAQG;AACH,qBAAa,aAAc,YAAW,SAAS;IAW/B,QAAQ,CAAC,OAAO,EAAE,MAAM;IAAM,QAAQ,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC;IAT9D,MAAM,CAAC,eAAe,SAAK;IAE3B,uBAAuB;IAChB,GAAG,EAAE,MAAM,CAAK;IAEvB;;;OAGG;gBACkB,OAAO,GAAE,MAAU,EAAW,IAAI,GAAE,CAAC,GAAG,CAAK;IAElE,IAAW,QAAQ,WAA2D;IAE9E,wDAAwD;IACxD,IAAW,KAAK,WAMf;IAED,IAAW,QAAQ,WAMlB;IAED,uDAAuD;IACvD,IAAW,IAAI,WAOd;IAED,IAAW,GAAG,WAEb;IAEM,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,GAAE,MAAkB,EAAE,GAAG,GAAE,MAAkB;IAOpE,OAAO,IAAI,SAAS;CAO9B;AAED;;;EAGE;AACF,wBAAgB,cAAc,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,UAEjF;AAED,oDAAoD;AACpD,wBAAgB,UAAU,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,UAE7E"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.basedFloor = exports.basedRemainder = exports.CalendarIndex = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Helps with converting sequential index to 0- or 1-based month, week, day index.
|
|
6
|
+
*
|
|
7
|
+
* Allows to use any number of days per week, not just 7. Also one can set any number of weeks per month (but it will be used globally).
|
|
8
|
+
*
|
|
9
|
+
* To update the value, either set `raw` property or use `set` method.
|
|
10
|
+
*
|
|
11
|
+
* Usage case: some ordered sequence of items, each has its own index, and you want to distribute them by months, weeks and days, knowing the number of items assigned per week.
|
|
12
|
+
*/
|
|
13
|
+
class CalendarIndex {
|
|
14
|
+
perWeek;
|
|
15
|
+
base;
|
|
16
|
+
static WEEKS_PER_MONTH = 4;
|
|
17
|
+
/** sequential index */
|
|
18
|
+
raw = 0;
|
|
19
|
+
/**
|
|
20
|
+
* @param perWeek how many indexes allocated per week
|
|
21
|
+
* @param [base=0] 1 or 0 - determines base for all indexes (0 by default)
|
|
22
|
+
*/
|
|
23
|
+
constructor(perWeek = 7, base = 0) {
|
|
24
|
+
this.perWeek = perWeek;
|
|
25
|
+
this.base = base;
|
|
26
|
+
}
|
|
27
|
+
get perMonth() { return this.perWeek * CalendarIndex.WEEKS_PER_MONTH; }
|
|
28
|
+
/** 1/0-based month index based on `perWeek` property */
|
|
29
|
+
get month() {
|
|
30
|
+
if (this.raw <= 0) {
|
|
31
|
+
return 0;
|
|
32
|
+
}
|
|
33
|
+
return basedFloor(this.base, this.raw, this.perMonth);
|
|
34
|
+
}
|
|
35
|
+
get fullWeek() {
|
|
36
|
+
if (this.raw <= 0) {
|
|
37
|
+
return 0;
|
|
38
|
+
}
|
|
39
|
+
return basedFloor(this.base, this.raw, this.perWeek);
|
|
40
|
+
}
|
|
41
|
+
/** 1/0-based week index based on `perWeek` property */
|
|
42
|
+
get week() {
|
|
43
|
+
const fw = this.fullWeek;
|
|
44
|
+
if (fw === 0) {
|
|
45
|
+
return 0;
|
|
46
|
+
}
|
|
47
|
+
return basedRemainder(this.base, fw, CalendarIndex.WEEKS_PER_MONTH);
|
|
48
|
+
}
|
|
49
|
+
get day() {
|
|
50
|
+
return basedRemainder(this.base, this.raw, this.perWeek);
|
|
51
|
+
}
|
|
52
|
+
set(month, week = this.base, day = this.base) {
|
|
53
|
+
const m = Math.max(month - this.base, 0);
|
|
54
|
+
const w = Math.max(week - this.base, 0);
|
|
55
|
+
this.raw = m * this.perMonth + w * this.perWeek + day;
|
|
56
|
+
return this;
|
|
57
|
+
}
|
|
58
|
+
toIndex() {
|
|
59
|
+
return {
|
|
60
|
+
month: this.month,
|
|
61
|
+
week: this.week,
|
|
62
|
+
day: this.day,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
exports.CalendarIndex = CalendarIndex;
|
|
67
|
+
/** returns based (1/0) remainder of `value / div`
|
|
68
|
+
*
|
|
69
|
+
* Ex.: get day of week
|
|
70
|
+
*/
|
|
71
|
+
function basedRemainder(base, value, div) {
|
|
72
|
+
return (value - base) % div + base;
|
|
73
|
+
}
|
|
74
|
+
exports.basedRemainder = basedRemainder;
|
|
75
|
+
/** returns based (1/0) floor'ed of `value / div` */
|
|
76
|
+
function basedFloor(base, value, div) {
|
|
77
|
+
return Math.floor((value - base) / div) + base;
|
|
78
|
+
}
|
|
79
|
+
exports.basedFloor = basedFloor;
|
|
80
|
+
//# sourceMappingURL=calendarIndex.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"calendarIndex.js","sourceRoot":"","sources":["../../src/dates/calendarI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