date-picker-svelte
Advanced tools
Comparing version 2.12.0 to 2.13.0
@@ -14,4 +14,4 @@ import { SvelteComponentTyped } from "svelte"; | ||
/** Pass custom classes */ class?: string | undefined; | ||
/** Locale object for internationalization */ locale?: Locale | undefined; | ||
/** Format string */ format?: string | undefined; | ||
/** Locale object for internationalization */ locale?: Locale | undefined; | ||
text?: string | undefined; | ||
@@ -18,0 +18,0 @@ /** Whether the date popup is visible */ visible?: boolean | undefined; |
export type Locale = { | ||
weekdays?: string[]; | ||
months?: string[]; | ||
shortMonths?: string[]; | ||
weekStartsOn?: number; | ||
}; | ||
type InnerLocale = { | ||
export type InnerLocale = { | ||
weekdays: string[]; | ||
months: string[]; | ||
shortMonths: string[]; | ||
weekStartsOn: number; | ||
}; | ||
export declare function getLocaleDefaults(): InnerLocale; | ||
export declare function getInnerLocale(locale?: Locale): InnerLocale; | ||
export declare function getInnerLocale(locale: Locale): InnerLocale; | ||
type Month = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11; | ||
type Day = 0 | 1 | 2 | 3 | 4 | 5 | 6; | ||
type LocaleWidth = 'short' | 'wide' | 'abbreviated' | 'narrow' | 'any'; | ||
type DateFnsLocale = { | ||
@@ -18,7 +23,7 @@ options?: { | ||
localize?: { | ||
month: (n: number, options?: { | ||
width?: string; | ||
month: (n: Month, options?: { | ||
width?: LocaleWidth; | ||
}) => string; | ||
day: (i: number, options?: { | ||
width?: string; | ||
day: (i: Day, options?: { | ||
width?: LocaleWidth; | ||
}) => string; | ||
@@ -25,0 +30,0 @@ }; |
@@ -18,6 +18,20 @@ export function getLocaleDefaults() { | ||
], | ||
shortMonths: [ | ||
'Jan', | ||
'Feb', | ||
'Mar', | ||
'Apr', | ||
'May', | ||
'Jun', | ||
'Jul', | ||
'Aug', | ||
'Sep', | ||
'Oct', | ||
'Nov', | ||
'Dec', | ||
], | ||
weekStartsOn: 1, | ||
}; | ||
} | ||
export function getInnerLocale(locale = {}) { | ||
export function getInnerLocale(locale) { | ||
const innerLocale = getLocaleDefaults(); | ||
@@ -29,2 +43,4 @@ if (typeof locale.weekStartsOn === 'number') { | ||
innerLocale.months = locale.months; | ||
if (locale.shortMonths) | ||
innerLocale.shortMonths = locale.shortMonths; | ||
if (locale.weekdays) | ||
@@ -47,2 +63,3 @@ innerLocale.weekdays = locale.weekdays; | ||
locale.months[i] = dateFnsLocale.localize.month(i, { width: 'wide' }); | ||
locale.shortMonths[i] = dateFnsLocale.localize.month(i, { width: 'abbreviated' }); | ||
} | ||
@@ -49,0 +66,0 @@ } |
@@ -0,3 +1,5 @@ | ||
import { type Locale } from './locale'; | ||
type RuleToken = { | ||
id: string; | ||
allowedValues?: string[]; | ||
toString: (d: Date) => string; | ||
@@ -12,3 +14,3 @@ }; | ||
export declare function parse(str: string, tokens: FormatToken[], baseDate: Date | null): ParseResult; | ||
export declare function createFormat(s: string): FormatToken[]; | ||
export declare function createFormat(s: string, locale?: Locale): FormatToken[]; | ||
export {}; |
import { getMonthLength } from './date-utils.js'; | ||
import { getInnerLocale } from './locale'; | ||
/** Parse a string according to the supplied format tokens. Returns a date if successful, and the missing punctuation if there is any that should be after the string */ | ||
@@ -45,2 +46,15 @@ export function parse(str, tokens, baseDate) { | ||
} | ||
function parseEnum(allowedValues) { | ||
const n = allowedValues.findIndex((allowedValue) => { | ||
return allowedValue.toLowerCase() === str.slice(0, allowedValue.length).toLowerCase(); | ||
}); | ||
if (n >= 0) { | ||
str = str.slice(allowedValues[n].length); | ||
return n; | ||
} | ||
else { | ||
valid = false; | ||
return null; | ||
} | ||
} | ||
function parseToken(token) { | ||
@@ -65,2 +79,7 @@ if (typeof token === 'string') { | ||
} | ||
else if (token.id === 'MMM') { | ||
const value = parseEnum(token.allowedValues || []); | ||
if (value !== null) | ||
month = value; | ||
} | ||
else if (token.id === 'dd') { | ||
@@ -104,43 +123,58 @@ const value = parseUint(/^[0-9]{2}/, 1, 31); | ||
} | ||
const ruleTokens = [ | ||
{ | ||
id: 'yyyy', | ||
toString: (d) => d.getFullYear().toString(), | ||
}, | ||
{ | ||
id: 'yy', | ||
toString: (d) => d.getFullYear().toString().slice(-2), | ||
}, | ||
{ | ||
id: 'MM', | ||
toString: (d) => twoDigit(d.getMonth() + 1), | ||
}, | ||
{ | ||
id: 'dd', | ||
toString: (d) => twoDigit(d.getDate()), | ||
}, | ||
{ | ||
id: 'HH', | ||
toString: (d) => twoDigit(d.getHours()), | ||
}, | ||
{ | ||
id: 'mm', | ||
toString: (d) => twoDigit(d.getMinutes()), | ||
}, | ||
{ | ||
id: 'ss', | ||
toString: (d) => twoDigit(d.getSeconds()), | ||
}, | ||
]; | ||
function parseRule(s) { | ||
for (const token of ruleTokens) { | ||
if (s.startsWith(token.id)) { | ||
return token; | ||
} | ||
function parseRule(s, innerLocale) { | ||
if (s.startsWith('yyyy')) { | ||
return { | ||
id: 'yyyy', | ||
toString: (d) => d.getFullYear().toString(), | ||
}; | ||
} | ||
else if (s.startsWith('yy')) { | ||
return { | ||
id: 'yy', | ||
toString: (d) => d.getFullYear().toString().slice(-2), | ||
}; | ||
} | ||
else if (s.startsWith('MMM')) { | ||
return { | ||
id: 'MMM', | ||
allowedValues: innerLocale.shortMonths, | ||
toString: (d) => innerLocale.shortMonths[d.getMonth()], | ||
}; | ||
} | ||
else if (s.startsWith('MM')) { | ||
return { | ||
id: 'MM', | ||
toString: (d) => twoDigit(d.getMonth() + 1), | ||
}; | ||
} | ||
else if (s.startsWith('dd')) { | ||
return { | ||
id: 'dd', | ||
toString: (d) => twoDigit(d.getDate()), | ||
}; | ||
} | ||
else if (s.startsWith('HH')) { | ||
return { | ||
id: 'HH', | ||
toString: (d) => twoDigit(d.getHours()), | ||
}; | ||
} | ||
else if (s.startsWith('mm')) { | ||
return { | ||
id: 'mm', | ||
toString: (d) => twoDigit(d.getMinutes()), | ||
}; | ||
} | ||
else if (s.startsWith('ss')) { | ||
return { | ||
id: 'ss', | ||
toString: (d) => twoDigit(d.getSeconds()), | ||
}; | ||
} | ||
} | ||
export function createFormat(s) { | ||
export function createFormat(s, locale = {}) { | ||
const innerLocale = getInnerLocale(locale); | ||
const tokens = []; | ||
while (s.length > 0) { | ||
const token = parseRule(s); | ||
const token = parseRule(s, innerLocale); | ||
if (token) { | ||
@@ -147,0 +181,0 @@ // parsed a token like "yyyy" |
{ | ||
"name": "date-picker-svelte", | ||
"version": "2.12.0", | ||
"version": "2.13.0", | ||
"description": "Date and time picker for Svelte", | ||
@@ -19,21 +19,22 @@ "type": "module", | ||
"devDependencies": { | ||
"@sveltejs/adapter-static": "^2.0.3", | ||
"@sveltejs/kit": "^1.27.7", | ||
"@sveltejs/package": "^2.2.3", | ||
"@typescript-eslint/eslint-plugin": "^6.13.2", | ||
"@typescript-eslint/parser": "^6.13.2", | ||
"@vitest/coverage-v8": "^0.34.6", | ||
"date-fns": "^2.30.0", | ||
"eslint": "^8.55.0", | ||
"@sveltejs/adapter-static": "^3.0.1", | ||
"@sveltejs/kit": "^2.5.10", | ||
"@sveltejs/package": "^2.3.1", | ||
"@sveltejs/vite-plugin-svelte": "^3.1.0", | ||
"@typescript-eslint/eslint-plugin": "^7.10.0", | ||
"@typescript-eslint/parser": "^7.10.0", | ||
"@vitest/coverage-v8": "^1.6.0", | ||
"date-fns": "^3.6.0", | ||
"eslint": "^8.57.0", | ||
"eslint-config-prettier": "^9.1.0", | ||
"eslint-plugin-svelte": "^2.35.1", | ||
"eslint-plugin-svelte": "^2.39.0", | ||
"mdsvex": "^0.11.0", | ||
"prettier": "^3.1.0", | ||
"prettier-plugin-svelte": "^3.1.2", | ||
"sass": "^1.69.5", | ||
"svelte": "^4.2.8", | ||
"svelte-check": "^3.6.2", | ||
"typescript": "^5.3.2", | ||
"vite": "^4.5.1", | ||
"vitest": "^0.34.6" | ||
"prettier": "^3.2.5", | ||
"prettier-plugin-svelte": "^3.2.3", | ||
"sass": "^1.77.2", | ||
"svelte": "^4.2.17", | ||
"svelte-check": "^3.7.1", | ||
"typescript": "^5.4.5", | ||
"vite": "^5.2.11", | ||
"vitest": "^1.6.0" | ||
}, | ||
@@ -40,0 +41,0 @@ "peerDependencies": { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
47794
469
20