@rjsf/utils 5.18.6 → 5.19.0
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.js +28 -9
- package/dist/index.js.map +3 -3
- package/dist/utils.esm.js +28 -9
- package/dist/utils.esm.js.map +3 -3
- package/dist/utils.umd.js +28 -9
- package/lib/dateRangeOptions.d.ts +11 -0
- package/lib/dateRangeOptions.js +28 -0
- package/lib/dateRangeOptions.js.map +1 -0
- package/lib/index.d.ts +2 -1
- package/lib/index.js +2 -1
- package/lib/index.js.map +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/src/dateRangeOptions.ts +31 -0
- package/src/index.ts +2 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import pad from './pad';
|
|
2
|
+
import { EnumOptionsType, RJSFSchema, StrictRJSFSchema } from './types';
|
|
3
|
+
|
|
4
|
+
/** Returns a list of options for a date range between `start` and `stop`. If the start date is greater than the end
|
|
5
|
+
* date, then the date range is reversed. If `start` and `stop` are negative numbers (or zero), then they will be
|
|
6
|
+
* treated as relative to the current year.
|
|
7
|
+
*
|
|
8
|
+
* @param start - The starting point of the date range
|
|
9
|
+
* @param stop - The ending point of the date range
|
|
10
|
+
* @returns - The list of EnumOptionsType for the date range between `start` and `stop`
|
|
11
|
+
* @throws - Error when `start` and `stop` aren't both <= 0 or > 0
|
|
12
|
+
*/
|
|
13
|
+
export default function dateRangeOptions<S extends StrictRJSFSchema = RJSFSchema>(
|
|
14
|
+
start: number,
|
|
15
|
+
stop: number
|
|
16
|
+
): EnumOptionsType<S>[] {
|
|
17
|
+
if (start <= 0 && stop <= 0) {
|
|
18
|
+
start = new Date().getFullYear() + start;
|
|
19
|
+
stop = new Date().getFullYear() + stop;
|
|
20
|
+
} else if (start < 0 || stop < 0) {
|
|
21
|
+
throw new Error(`Both start (${start}) and stop (${stop}) must both be <= 0 or > 0, got one of each`);
|
|
22
|
+
}
|
|
23
|
+
if (start > stop) {
|
|
24
|
+
return dateRangeOptions<S>(stop, start).reverse();
|
|
25
|
+
}
|
|
26
|
+
const options: EnumOptionsType<S>[] = [];
|
|
27
|
+
for (let i = start; i <= stop; i++) {
|
|
28
|
+
options.push({ value: i, label: pad(i, 2) });
|
|
29
|
+
}
|
|
30
|
+
return options;
|
|
31
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -4,6 +4,7 @@ import canExpand from './canExpand';
|
|
|
4
4
|
import createErrorHandler from './createErrorHandler';
|
|
5
5
|
import createSchemaUtils from './createSchemaUtils';
|
|
6
6
|
import dataURItoBlob from './dataURItoBlob';
|
|
7
|
+
import dateRangeOptions from './dateRangeOptions';
|
|
7
8
|
import deepEquals from './deepEquals';
|
|
8
9
|
import englishStringTranslator from './englishStringTranslator';
|
|
9
10
|
import enumOptionsDeselectValue from './enumOptionsDeselectValue';
|
|
@@ -68,6 +69,7 @@ export {
|
|
|
68
69
|
createSchemaUtils,
|
|
69
70
|
DateElementFormat,
|
|
70
71
|
dataURItoBlob,
|
|
72
|
+
dateRangeOptions,
|
|
71
73
|
deepEquals,
|
|
72
74
|
descriptionId,
|
|
73
75
|
englishStringTranslator,
|