scriptable-testlab
Version:
A lightweight, efficient tool designed to manage and update scripts for Scriptable.
70 lines (61 loc) • 1.63 kB
text/typescript
import {AbsDateFormatter} from 'scriptable-abstract';
/**
* Mock implementation of Scriptable's DateFormatter
* Used to format dates into strings and parse strings into dates
*/
export class MockDateFormatter extends AbsDateFormatter {
constructor() {
super({
locale: 'en-US',
dateFormat: '',
});
}
/**
* Gets the locale identifier
*/
get locale(): string {
return this.state.locale.replace('-', '_');
}
/**
* Sets the locale identifier
*/
set locale(value: string) {
// Convert Scriptable locale format to IETF language tag
const [lang, region] = value.toLowerCase().split('_');
this.setState({locale: `${lang}-${region?.toUpperCase() || ''}`.replace(/-$/, '')});
}
/**
* Gets the custom date format string
*/
get dateFormat(): string {
return this.state.dateFormat;
}
/**
* Sets the custom date format string
*/
set dateFormat(value: string) {
this.setState({dateFormat: value});
}
/**
* Formats a date into a string
* @param date Date to format
* @returns Formatted date string
*/
string(date: Date): string {
if (this.dateFormat) {
// If custom format is set, use it
// This is a simplified implementation - in real usage you'd want to
// convert Scriptable format string to Intl format
return date.toLocaleString(this.state.locale);
}
return date.toLocaleString(this.state.locale);
}
/**
* Parses a string into a date
* @param dateString String to parse
* @returns Parsed date
*/
date(dateString: string): Date {
return new Date(dateString);
}
}