cronli5 0.1.5 → 0.1.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cronli5",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "description": "Cron Like I'm Five: A Cron to English Utility",
5
5
  "repository": {
6
6
  "type": "git",
@@ -70,7 +70,7 @@
70
70
  "test:types": "npm run types && tsd",
71
71
  "typecheck": "tsc -p tsconfig.json",
72
72
  "coverage": "c8 mocha",
73
- "verify": "npm run lint && npm run typecheck && npm run test:types && npm test && npm run coverage && npm run docs -- --check && npm run build",
73
+ "verify": "npm run lint && npm run typecheck && npm run test:types && npm test && npm run coverage && npm run conciseness && npm run docs -- --check && npm run build",
74
74
  "prepare": "node scripts/install-hooks.mjs",
75
75
  "prepublishOnly": "npm run lint && npm run typecheck && npm run test:types && npm run build && npm test"
76
76
  },
package/src/core/util.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  // Small shared utilities for the core.
2
2
 
3
+ import type {Segment} from './ir.js';
4
+
3
5
  function includes(str: string | number, sub: string): boolean {
4
6
  return ('' + str).indexOf(sub) !== -1;
5
7
  }
@@ -44,6 +46,54 @@ function arithmeticStep(values: number[]):
44
46
  return {start: values[0], interval, last: values[values.length - 1]};
45
47
  }
46
48
 
49
+ // The display sort key for a canonical weekday number: Monday (1) first,
50
+ // Sunday (0) last. The IR keeps Sunday=0 canonical; this is display-only.
51
+ function weekdayDisplayKey(value: number): number {
52
+ return value === 0 ? 7 : value;
53
+ }
54
+
55
+ // A weekday display segment: a single day or a (possibly wrap) range. Steps
56
+ // are flattened away into singles before sorting, so the result is only these
57
+ // two kinds; each renderer turns them into names exactly as it does today.
58
+ type WeekdaySegment =
59
+ | {kind: 'single'; value: string}
60
+ | {kind: 'range'; bounds: [string, string]};
61
+
62
+ // Reorder weekday segments Monday-first (Sunday last) for display, so a weekend
63
+ // list reads "Saturday and Sunday" rather than the canonical Sunday-first
64
+ // "Sunday and Saturday". Display-only: the IR / canonical order is unchanged (a
65
+ // fresh array is returned). A step expands to its fires as singles so the days
66
+ // sort into the list; a range stays one unit and keeps its own bounds order (a
67
+ // wrap range is not reordered into a list), sorting by its opening bound — so a
68
+ // lone range sorts to a one-element list and is unchanged. The sort is stable,
69
+ // so equal opening days keep input order.
70
+ function orderWeekdaysForDisplay(segments: Segment[]): WeekdaySegment[] {
71
+ const flattened: WeekdaySegment[] = segments.flatMap(function flat(segment) {
72
+ return segment.kind === 'step' ?
73
+ segment.fires.map(function single(value): WeekdaySegment {
74
+ return {kind: 'single', value: '' + value};
75
+ }) :
76
+ [segment];
77
+ });
78
+
79
+ function key(segment: WeekdaySegment): number {
80
+ return segment.kind === 'range' ?
81
+ weekdayDisplayKey(+segment.bounds[0]) :
82
+ weekdayDisplayKey(+segment.value);
83
+ }
84
+
85
+ return flattened
86
+ .map(function index(segment, position): [WeekdaySegment, number] {
87
+ return [segment, position];
88
+ })
89
+ .sort(function byDisplayKey(a, b): number {
90
+ return key(a[0]) - key(b[0]) || a[1] - b[1];
91
+ })
92
+ .map(function unwrap(pair): WeekdaySegment {
93
+ return pair[0];
94
+ });
95
+ }
96
+
47
97
  // Resolve a numeric or named field token (e.g. '5' or 'FRI') to its number.
48
98
  function toFieldNumber(
49
99
  token: string,
@@ -54,5 +104,6 @@ function toFieldNumber(
54
104
  return isNonNegativeInteger(token) ? +token : numberMap![token.toUpperCase()];
55
105
  }
56
106
  export {
57
- arithmeticStep, includes, isNonNegativeInteger, toFieldNumber, unique
107
+ arithmeticStep, includes, isNonNegativeInteger, orderWeekdaysForDisplay,
108
+ toFieldNumber, unique
58
109
  };