@nervure/datetime 0.0.0-stage → 0.2.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/vue.d.ts ADDED
@@ -0,0 +1,72 @@
1
+ import { type PropType } from 'vue';
2
+ import type { Mode, PickerOptions, PickerValue, Selection } from './types.ts';
3
+ export type { Comparison, Granularity, Mode, Picker, PickerOptions, PickerValue, Selection } from './types.ts';
4
+ /** `<NvDateTime v-model="period" mode="range" presets locale="fr" />`. `change` gives the whole selection. */
5
+ export declare const NvDateTime: import("vue").DefineComponent<import("vue").ExtractPropTypes<{
6
+ modelValue: {
7
+ type: PropType<PickerValue | Date>;
8
+ default: undefined;
9
+ };
10
+ mode: {
11
+ type: PropType<Mode>;
12
+ default: undefined;
13
+ };
14
+ /** Every other option of the picker: zone, zones, locale, min, max, months, time, presets, format, disable... */
15
+ options: {
16
+ type: PropType<PickerOptions>;
17
+ default: () => {};
18
+ };
19
+ inline: {
20
+ type: BooleanConstructor;
21
+ default: boolean;
22
+ };
23
+ name: {
24
+ type: StringConstructor;
25
+ default: undefined;
26
+ };
27
+ disabled: {
28
+ type: BooleanConstructor;
29
+ default: boolean;
30
+ };
31
+ }>, () => import("vue").VNode<import("vue").RendererNode, import("vue").RendererElement, {
32
+ [key: string]: any;
33
+ }>, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
34
+ 'update:modelValue': (_value: PickerValue) => true;
35
+ change: (_selection: Selection) => true;
36
+ }, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{
37
+ modelValue: {
38
+ type: PropType<PickerValue | Date>;
39
+ default: undefined;
40
+ };
41
+ mode: {
42
+ type: PropType<Mode>;
43
+ default: undefined;
44
+ };
45
+ /** Every other option of the picker: zone, zones, locale, min, max, months, time, presets, format, disable... */
46
+ options: {
47
+ type: PropType<PickerOptions>;
48
+ default: () => {};
49
+ };
50
+ inline: {
51
+ type: BooleanConstructor;
52
+ default: boolean;
53
+ };
54
+ name: {
55
+ type: StringConstructor;
56
+ default: undefined;
57
+ };
58
+ disabled: {
59
+ type: BooleanConstructor;
60
+ default: boolean;
61
+ };
62
+ }>> & Readonly<{
63
+ onChange?: ((_selection: Selection) => any) | undefined;
64
+ "onUpdate:modelValue"?: ((_value: PickerValue) => any) | undefined;
65
+ }>, {
66
+ mode: Mode;
67
+ name: string;
68
+ disabled: boolean;
69
+ inline: boolean;
70
+ options: PickerOptions;
71
+ modelValue: Date | PickerValue;
72
+ }, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
package/dist/vue.js ADDED
@@ -0,0 +1,49 @@
1
+ import { defineComponent, h, onBeforeUnmount, onMounted, ref, watch } from 'vue';
2
+ import { createDatePicker } from "./core.js";
3
+ /** `<NvDateTime v-model="period" mode="range" presets locale="fr" />`. `change` gives the whole selection. */
4
+ export const NvDateTime = defineComponent({
5
+ name: 'NvDateTime',
6
+ props: {
7
+ modelValue: { type: [String, Array, Object, Date], default: undefined },
8
+ mode: { type: String, default: undefined },
9
+ /** Every other option of the picker: zone, zones, locale, min, max, months, time, presets, format, disable... */
10
+ options: { type: Object, default: () => ({}) },
11
+ inline: { type: Boolean, default: false },
12
+ name: { type: String, default: undefined },
13
+ disabled: { type: Boolean, default: false },
14
+ },
15
+ emits: {
16
+ 'update:modelValue': (_value) => true,
17
+ change: (_selection) => true,
18
+ },
19
+ setup(props, { emit, expose }) {
20
+ const host = ref(null);
21
+ let picker = null;
22
+ const settings = () => ({
23
+ ...props.options,
24
+ ...(props.mode ? { mode: props.mode } : {}),
25
+ inline: props.inline || props.options.inline,
26
+ });
27
+ onMounted(() => {
28
+ picker = createDatePicker(host.value, {
29
+ ...settings(),
30
+ value: props.modelValue,
31
+ onChange: (selection) => {
32
+ props.options.onChange?.(selection);
33
+ emit('update:modelValue', selection.value);
34
+ emit('change', selection);
35
+ },
36
+ });
37
+ });
38
+ onBeforeUnmount(() => picker?.destroy());
39
+ watch(() => [props.options, props.mode], () => picker?.update(settings()), { deep: true });
40
+ watch(() => props.modelValue, (value) => {
41
+ if (picker && value !== undefined && JSON.stringify(picker.getValue()) !== JSON.stringify(value))
42
+ picker.setValue(value);
43
+ }, { deep: true });
44
+ expose({ picker: () => picker });
45
+ return () => props.inline || props.options.inline
46
+ ? h('div', { ref: host })
47
+ : h('input', { ref: host, type: 'text', name: props.name, disabled: props.disabled });
48
+ },
49
+ });
package/dist/zone.d.ts ADDED
@@ -0,0 +1,22 @@
1
+ import type { PlainDate, PlainTime } from './calendar.ts';
2
+ /** The zone of the browser or the server. */
3
+ export declare const localZone: () => string;
4
+ /** `local` becomes the zone of the device; `utc` in any case becomes `UTC`. */
5
+ export declare const resolveZone: (zone: string | undefined) => string;
6
+ /** True when the platform knows the zone (an IANA name such as `Europe/Paris`). */
7
+ export declare function isZone(zone: string): boolean;
8
+ /** The wall clock of a zone at an instant. */
9
+ export declare function wallClock(epoch: number, zone: string): PlainDate & PlainTime;
10
+ /** Minutes to add to UTC to get the wall clock of the zone at this instant (Paris in winter: 60). */
11
+ export declare function offsetMinutes(epoch: number, zone: string): number;
12
+ /**
13
+ * The instant of a wall clock in a zone. An hour that does not exist (spring change) moves forward;
14
+ * an hour that comes twice (autumn change) takes the first one.
15
+ */
16
+ export declare function instant(date: PlainDate, time: PlainTime, zone: string): number;
17
+ /** Today in a zone. */
18
+ export declare function today(zone: string, now?: number): PlainDate;
19
+ /** `+01:00`, `-05:30`, or `Z` for UTC. */
20
+ export declare function offsetText(epoch: number, zone: string): string;
21
+ /** Short name of the zone at an instant, in the language of the reader: `UTC+1`, `CET`, `GMT-5`. */
22
+ export declare function zoneLabel(epoch: number, zone: string, locale: string): string;
package/dist/zone.js ADDED
@@ -0,0 +1,81 @@
1
+ const formats = new Map();
2
+ /** The zone of the browser or the server. */
3
+ export const localZone = () => new Intl.DateTimeFormat().resolvedOptions().timeZone;
4
+ /** `local` becomes the zone of the device; `utc` in any case becomes `UTC`. */
5
+ export const resolveZone = (zone) => !zone || zone === 'local' ? localZone() : zone.toUpperCase() === 'UTC' ? 'UTC' : zone;
6
+ /** True when the platform knows the zone (an IANA name such as `Europe/Paris`). */
7
+ export function isZone(zone) {
8
+ try {
9
+ new Intl.DateTimeFormat('en-US', { timeZone: zone });
10
+ return true;
11
+ }
12
+ catch {
13
+ return false;
14
+ }
15
+ }
16
+ function format(zone) {
17
+ let f = formats.get(zone);
18
+ if (!f) {
19
+ f = new Intl.DateTimeFormat('en-US', {
20
+ timeZone: zone,
21
+ hourCycle: 'h23',
22
+ year: 'numeric',
23
+ month: '2-digit',
24
+ day: '2-digit',
25
+ hour: '2-digit',
26
+ minute: '2-digit',
27
+ second: '2-digit',
28
+ });
29
+ formats.set(zone, f);
30
+ }
31
+ return f;
32
+ }
33
+ /** The wall clock of a zone at an instant. */
34
+ export function wallClock(epoch, zone) {
35
+ const out = {};
36
+ for (const part of format(zone).formatToParts(epoch))
37
+ if (part.type !== 'literal')
38
+ out[part.type] = Number(part.value);
39
+ return { y: out.year, m: out.month, d: out.day, h: out.hour % 24, mi: out.minute, s: out.second };
40
+ }
41
+ /** Minutes to add to UTC to get the wall clock of the zone at this instant (Paris in winter: 60). */
42
+ export function offsetMinutes(epoch, zone) {
43
+ const w = wallClock(epoch, zone);
44
+ return Math.round((Date.UTC(w.y, w.m - 1, w.d, w.h, w.mi, w.s) - Math.floor(epoch / 1000) * 1000) / 60_000);
45
+ }
46
+ /**
47
+ * The instant of a wall clock in a zone. An hour that does not exist (spring change) moves forward;
48
+ * an hour that comes twice (autumn change) takes the first one.
49
+ */
50
+ export function instant(date, time, zone) {
51
+ const wall = Date.UTC(date.y, date.m - 1, date.d, time.h, time.mi, time.s);
52
+ const guess = wall - offsetMinutes(wall, zone) * 60_000;
53
+ const later = wall - offsetMinutes(guess, zone) * 60_000;
54
+ // The offset in force three hours earlier: it gives the first of two equal hours when the clock goes back
55
+ const before = offsetMinutes(later - 3 * 3_600_000, zone);
56
+ const earlier = wall - before * 60_000;
57
+ return earlier < later && offsetMinutes(earlier, zone) === before ? earlier : later;
58
+ }
59
+ /** Today in a zone. */
60
+ export function today(zone, now = Date.now()) {
61
+ const w = wallClock(now, zone);
62
+ return { y: w.y, m: w.m, d: w.d };
63
+ }
64
+ /** `+01:00`, `-05:30`, or `Z` for UTC. */
65
+ export function offsetText(epoch, zone) {
66
+ const minutes = offsetMinutes(epoch, zone);
67
+ if (zone === 'UTC' || minutes === 0)
68
+ return zone === 'UTC' ? 'Z' : '+00:00';
69
+ const abs = Math.abs(minutes);
70
+ return `${minutes < 0 ? '-' : '+'}${String(Math.floor(abs / 60)).padStart(2, '0')}:${String(abs % 60).padStart(2, '0')}`;
71
+ }
72
+ /** Short name of the zone at an instant, in the language of the reader: `UTC+1`, `CET`, `GMT-5`. */
73
+ export function zoneLabel(epoch, zone, locale) {
74
+ try {
75
+ const parts = new Intl.DateTimeFormat(locale, { timeZone: zone, timeZoneName: 'shortOffset' }).formatToParts(epoch);
76
+ return parts.find((p) => p.type === 'timeZoneName')?.value ?? zone;
77
+ }
78
+ catch {
79
+ return zone;
80
+ }
81
+ }
package/package.json CHANGED
@@ -1,6 +1,104 @@
1
1
  {
2
2
  "name": "@nervure/datetime",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.0",
4
+ "description": "Date and time picker from the nervure family: one day, a range, several days, a week, a month, a quarter, a year or a time, in any time zone and language. Framework-free, web component, React and Vue. No dependency.",
5
+ "keywords": [
6
+ "nervure",
7
+ "datepicker",
8
+ "date picker",
9
+ "datetime",
10
+ "datetime picker",
11
+ "date range picker",
12
+ "period picker",
13
+ "dashboard date filter",
14
+ "time picker",
15
+ "month picker",
16
+ "quarter picker",
17
+ "week picker",
18
+ "timezone",
19
+ "time zone",
20
+ "i18n",
21
+ "rtl",
22
+ "calendar",
23
+ "web component",
24
+ "custom element",
25
+ "react",
26
+ "vue",
27
+ "accessible",
28
+ "typescript",
29
+ "no dependency"
30
+ ],
31
+ "license": "MIT",
32
+ "author": "Ali Ben Macha <contact@benmacha.tn>",
33
+ "homepage": "https://nervurejs.github.io/datetime",
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "git+https://github.com/nervurejs/nervure.git",
37
+ "directory": "packages/datetime"
38
+ },
39
+ "bugs": "https://github.com/nervurejs/nervure/issues",
40
+ "type": "module",
41
+ "sideEffects": [
42
+ "./dist/element.js",
43
+ "./cdn/*"
44
+ ],
45
+ "files": [
46
+ "dist",
47
+ "cdn",
48
+ "README.md",
49
+ "CHANGELOG.md",
50
+ "LICENSE"
51
+ ],
52
+ "exports": {
53
+ ".": {
54
+ "types": "./dist/index.d.ts",
55
+ "default": "./dist/index.js"
56
+ },
57
+ "./core": {
58
+ "types": "./dist/core.d.ts",
59
+ "default": "./dist/core.js"
60
+ },
61
+ "./element": {
62
+ "types": "./dist/element.d.ts",
63
+ "default": "./dist/element.js"
64
+ },
65
+ "./react": {
66
+ "types": "./dist/react.d.ts",
67
+ "default": "./dist/react.js"
68
+ },
69
+ "./vue": {
70
+ "types": "./dist/vue.d.ts",
71
+ "default": "./dist/vue.js"
72
+ },
73
+ "./style.css": "./dist/style.css",
74
+ "./package.json": "./package.json"
75
+ },
76
+ "unpkg": "./cdn/nervure-datetime.min.js",
77
+ "jsdelivr": "./cdn/nervure-datetime.min.js",
78
+ "scripts": {
79
+ "build": "rm -rf dist cdn && tsc -p tsconfig.build.json && node scripts/build.mjs",
80
+ "test": "vitest run",
81
+ "types": "tsc -p tsconfig.json",
82
+ "size": "node scripts/size.mjs",
83
+ "check": "npm run types && npm run build && npm test && npm run size",
84
+ "prepublishOnly": "npm run check"
85
+ },
86
+ "peerDependencies": {
87
+ "react": ">=18",
88
+ "vue": ">=3.3"
89
+ },
90
+ "peerDependenciesMeta": {
91
+ "react": {
92
+ "optional": true
93
+ },
94
+ "vue": {
95
+ "optional": true
96
+ }
97
+ },
98
+ "publishConfig": {
99
+ "access": "public"
100
+ },
101
+ "engines": {
102
+ "node": ">=20"
103
+ }
104
+ }