All files / src/reactivity date.js

97.03% Statements 131/135
93.1% Branches 27/29
100% Functions 7/7
96.96% Lines 128/132

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 1332x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 14x 14x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 94x 58x 2x 2x 2x 2x 2x 2x 2x 16x 44x 44x 151x 151x 151x 151x 151x 35x 107x 107x 107x 35x 35x 35x 151x 151x 151x 44x 44x 2x 16x 14x 14x 48x 48x 48x 14x 14x 2x 16x 32x 32x 46x 46x 46x 46x 37x 37x 46x 32x 32x 2x 16x 2x 2x 2x 2x 2x 2x 2x 2x 2x 151x 151x 151x 151x 66x 151x         151x 151x 151x  
/**
 * @import { Source } from "#client"
 */
import { derived } from '../internal/client/index.js';
import { source, set } from '../internal/client/reactivity/sources.js';
import { get } from '../internal/client/runtime.js';
 
/**
 * @typedef {Exclude<{[Key in keyof Date]: Date[Key] extends Function ? Key : never}[keyof Date], symbol>} DateMethodNames
 */
 
var inited = false;
export class ReactiveDate extends Date {
	/**
	 * @type {Source<number>}
	 */
	#raw_time = source(super.getTime());
 
	/**
	 * @type {Map<DateMethodNames, Source<unknown>>}
	 */
	#deriveds = new Map();
 
	/**
	 * @param {any[]} params
	 */
	constructor(...params) {
		// @ts-ignore
		super(...params);
		this.#init();
	}
 
	#init() {
		if (inited) {
			return;
		}
 
		inited = true;
		var reactive_date_proto = ReactiveDate.prototype;
		var date_proto = Date.prototype;
 
		/**
		 * @type {Array<DateMethodNames>}
		 */
		var versioned_read_props = [
			'getTimezoneOffset',
			'getTime',
			'toISOString',
			'toLocaleString',
			'toJSON',
			'toUTCString',
			'toString'
		];
 
		var fine_grained_read_props = /** @type {Array<DateMethodNames>} */ (
			Object.getOwnPropertyNames(Date.prototype).filter(
				(prop) =>
					(prop.startsWith('get') || prop.startsWith('to')) &&
					!versioned_read_props.includes(/** @type {DateMethodNames} */ (prop))
			)
		);
 
		var write_props = /** @type {Array<DateMethodNames>} */ (
			Object.getOwnPropertyNames(Date.prototype).filter((prop) => prop.startsWith('set'))
		);
 
		for (const method of fine_grained_read_props) {
			// @ts-ignore
			reactive_date_proto[method] = function (...args) {
				// methods like getMinutes and getUTCMinutes can use the same signal
				// we strip getUTCMinutes to getMinutes so that they always result into the same signal
				var stripped_method_name = strip_local_and_utc(fine_grained_read_props, method);
				var sig = this.#deriveds.get(stripped_method_name);
				if (!sig) {
					sig = derived(() => {
						get(this.#raw_time);
						// @ts-ignore
						return date_proto[stripped_method_name].apply(this, args);
					});
					this.#deriveds.set(stripped_method_name, sig);
				}
				var result = get(sig);
				// @ts-ignore
				return method === stripped_method_name ? result : date_proto[method].apply(this, args);
			};
		}
 
		for (const method of versioned_read_props) {
			// @ts-ignore
			reactive_date_proto[method] = function (...args) {
				get(this.#raw_time);
				// @ts-ignore
				return date_proto[method].apply(this, args);
			};
		}
 
		for (const method of write_props) {
			// @ts-ignore
			reactive_date_proto[method] = function (...args) {
				// @ts-ignore
				var result = date_proto[method].apply(this, args);
				var new_time = date_proto.getTime.call(this);
				if (this.#raw_time.v !== new_time) {
					set(this.#raw_time, date_proto.getTime.call(this));
				}
				return result;
			};
		}
	}
}
 
/**
 * if there is a method with exactly the same name but without `UTC` or `Locale`,
 * it will return that one otherwise returns the original `method_name`.
 * for instance for `getUTCMonth` we will get `getMonth` and for `toLocaleDateString` we will get `toDateString`
 * @param {Array<DateMethodNames>} all_method_names
 * @param {DateMethodNames} method_name
 * @return {DateMethodNames}
 */
function strip_local_and_utc(all_method_names, method_name) {
	var modified_method_name = method_name;
 
	if (method_name.includes('UTC')) {
		modified_method_name = /**@type {DateMethodNames}*/ (modified_method_name.replace('UTC', ''));
	} else if (method_name.includes('Locale')) {
		modified_method_name = /**@type {DateMethodNames}*/ (
			modified_method_name.replace('Locale', '')
		);
	}
 
	return all_method_names.includes(modified_method_name) ? modified_method_name : method_name;
}