@tempo-kit/plugin-snap 0.0.0-stage → 2.0.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/CHANGELOG.md +77 -0
- package/LICENSE +21 -0
- package/README.md +48 -2
- package/dist/index.d.ts +43 -0
- package/dist/index.global.min.js +1 -0
- package/dist/index.js +126 -0
- package/dist/install.d.ts +1 -0
- package/dist/install.global.min.js +1 -0
- package/dist/install.js +129 -0
- package/package.json +60 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [2.0.0] - 2026-11-27
|
|
9
|
+
|
|
10
|
+
### Breaking Changes
|
|
11
|
+
- Migrated package namespace from `@magmacomputing/tempo-plugin-snap` to `@tempo-kit/plugin-snap`.
|
|
12
|
+
- Updated `peerDependencies` to `tempo-kit@^5.0.0`.
|
|
13
|
+
|
|
14
|
+
## [2.0.0] - 2026-11-27
|
|
15
|
+
|
|
16
|
+
### Breaking Changes
|
|
17
|
+
- Migrated package namespace from `@magmacomputing/tempo-plugin-snap` to `@tempo-kit/plugin-snap`.
|
|
18
|
+
- Updated `peerDependencies` to `tempo-kit@^5.0.0`.
|
|
19
|
+
|
|
20
|
+
## [1.4.2] - 2026-10-12
|
|
21
|
+
|
|
22
|
+
### Maintenance & Hardening
|
|
23
|
+
- **Type Union Standardization**: Cleaned up whitespace and union formatting for `SnapKey` (`hh`, `mi`, `ss`, `ms`, `us`, `ns` and their singular/plural aliases).
|
|
24
|
+
- **Core Compatibility**: Verified runtime interoperability and build pipeline stability against Tempo v4.6.0.
|
|
25
|
+
|
|
26
|
+
## [1.4.1] - 2026-10-06
|
|
27
|
+
|
|
28
|
+
### Fixed
|
|
29
|
+
- **Standardized Diagnostic Logging & Error Boundaries**:
|
|
30
|
+
- Routed error messages in catch mode through [`logError`](../../tempo/src/plugin/plugin.sdk.ts) from `@magmacomputing/tempo/plugin/sdk`.
|
|
31
|
+
- Added support for `{ silent: true }` configuration to suppress expected error output during tests and headless tasks.
|
|
32
|
+
|
|
33
|
+
## [1.4.0] - 2026-09-18
|
|
34
|
+
|
|
35
|
+
### Added
|
|
36
|
+
- **Subpath Side-Effect Installation (`/install`)**:
|
|
37
|
+
- Added dedicated `@tempo-kit/plugin-snap/install` entrypoint for zero-boilerplate side-effect registration on the global `Tempo` instance via `autoInstall`.
|
|
38
|
+
- Configured `package.json` with `"sideEffects": ["./dist/install.js"]` to ensure the root package entrypoint remains 100% pure and tree-shakeable.
|
|
39
|
+
|
|
40
|
+
## [1.3.3] - 2026-08-20
|
|
41
|
+
|
|
42
|
+
### Fixed
|
|
43
|
+
- **Peer Dependencies**: Expanded peerDependency range to support Tempo v4.0.0.
|
|
44
|
+
|
|
45
|
+
## [1.3.1] - 2026-07-15
|
|
46
|
+
|
|
47
|
+
### Fixed
|
|
48
|
+
- **Sub-Second Precision Math**: The fractional rounding algorithm now accurately factors in all lower-level active components (e.g., nanoseconds now properly drift into milliseconds) for perfect precision.
|
|
49
|
+
- **Safety Enforcer**: Added strict checks against `NaN` and `Infinity` inputs via `Number.isFinite()` to prevent infinite loops and math corruption.
|
|
50
|
+
|
|
51
|
+
## [1.3.0] - 2026-07-08
|
|
52
|
+
|
|
53
|
+
### Added
|
|
54
|
+
- **Directional Snapping**: Added an optional `direction` parameter (`'up'` or `'down'`) to forcefully snap times using `Math.ceil` or `Math.floor` instead of standard round-to-nearest behavior.
|
|
55
|
+
|
|
56
|
+
### Fixed
|
|
57
|
+
- **Plugin API Import**: Updated `definePlugin` import to correctly point to the unified `@magmacomputing/tempo/plugin-api` barrel export.
|
|
58
|
+
|
|
59
|
+
## [1.2.0] - 2026-07-08
|
|
60
|
+
|
|
61
|
+
### Added
|
|
62
|
+
- **Sub-Second Snapping**: Added support for snapping to sub-second precision (`ms`, `us`, `ns`) to support telemetry aggregation, multimedia synchronization, and database timestamp normalization.
|
|
63
|
+
|
|
64
|
+
### Fixed
|
|
65
|
+
|
|
66
|
+
- **Version Tracking**: Plugins will now register their 'version' with Tempo.versions
|
|
67
|
+
|
|
68
|
+
## [1.1.0] - 2026-07-07
|
|
69
|
+
|
|
70
|
+
### Changed
|
|
71
|
+
|
|
72
|
+
- **Idiomatic Argument Syntax**: Refactored the `snap` method signature from positional arguments (`t.snap('mi', 15)`) to a standard object-based API (`t.snap({ mi: 15 })`) to natively align with Tempo's core mutation syntax.
|
|
73
|
+
- **Shorthand Aliases**: Expanded the parameter typing to officially support long-form time aliases (e.g., `hours`, `minutes`, `seconds`) in addition to the standard shorthand keys (`hh`, `mi`, `ss`).
|
|
74
|
+
|
|
75
|
+
### Fixed
|
|
76
|
+
|
|
77
|
+
- **Runtime Validation**: Implemented strict runtime validation (powered by an internal `OneKey` utility type) to guarantee that only a single time component can be snapped at once. Passing date components (like `days` or `years`) will now safely throw explicit validation errors instead of silently failing.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Magma Computing
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,49 @@
|
|
|
1
|
-
|
|
1
|
+

|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
# @tempo-kit/plugin-snap
|
|
4
|
+
|
|
5
|
+
<p align="center">
|
|
6
|
+
<a href="https://www.npmjs.com/package/@tempo-kit/plugin-snap"><img src="https://img.shields.io/npm/v/@tempo-kit/plugin-snap?style=flat-square" alt="npm version" style="display: inline-block; margin: 0 4px;"></a> <a href="https://www.npmjs.com/package/tempo-kit"><img src="https://img.shields.io/npm/dependency-version/@tempo-kit/plugin-snap/peer/tempo-kit?style=flat-square" alt="npm peer dependency version" style="display: inline-block; margin: 0 4px;"></a> <a href="https://www.npmjs.com/package/@tempo-kit/plugin-snap"><img src="https://img.shields.io/npm/l/@tempo-kit/plugin-snap?style=flat-square" alt="License" style="display: inline-block; margin: 0 4px;"></a> <a href="https://www.typescriptlang.org/"><img src="https://img.shields.io/badge/TypeScript-Ready-blue?logo=typescript&style=flat-square" alt="TypeScript Ready" style="display: inline-block; margin: 0 4px;"></a> <a href="https://magmacomputing.github.io/magma/doc/9-plugins/snap.index.html"><img src="https://img.shields.io/badge/Docs-VitePress-brightgreen?logo=vitepress&style=flat-square" alt="Documentation" style="display: inline-block; margin: 0 4px;"></a>
|
|
7
|
+
</p>
|
|
8
|
+
|
|
9
|
+
A Community plugin for the [Tempo](https://github.com/magmacomputing/magma) library that provides robust time rounding and snapping functionality (e.g. snapping to the nearest 15-minute or 1-hour block) for calendar and scheduling applications.
|
|
10
|
+
|
|
11
|
+
👉 **[View the full documentation on our GitHub Pages](https://magmacomputing.github.io/magma/doc/9-plugins/snap.index.html)**
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @tempo-kit/plugin-snap
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { Tempo } from 'tempo-kit';
|
|
23
|
+
import { SnapPlugin } from '@tempo-kit/plugin-snap';
|
|
24
|
+
|
|
25
|
+
// Pass the plugin to `Tempo.init` to register it into the runtime.
|
|
26
|
+
Tempo.init({
|
|
27
|
+
extends: [SnapPlugin]
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const t = new Tempo('2026-06-01T14:08:00Z');
|
|
31
|
+
|
|
32
|
+
// Snaps to the nearest 15 minutes by default
|
|
33
|
+
const snapped = t.snap();
|
|
34
|
+
console.log(snapped.format('{hh}:{mi}')); // "14:15"
|
|
35
|
+
|
|
36
|
+
// Or explicitly provide units and intervals
|
|
37
|
+
const snapHour = t.snap('hh', 1);
|
|
38
|
+
const snapSecond = t.snap('ss', 30);
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
> ⚡ **[Try this live in the interactive Tempo Sandbox ↗](https://magmacomputing.github.io/magma/repl/index.html?plugin=snap)**
|
|
42
|
+
|
|
43
|
+
## Documentation
|
|
44
|
+
|
|
45
|
+
For full API reference, advanced configuration, and detailed explanations of the rounding features, please visit the official 📖 **[Snap Plugin Documentation](https://magmacomputing.github.io/magma/doc/9-plugins/snap.index.html)**.
|
|
46
|
+
|
|
47
|
+
## Licensing
|
|
48
|
+
|
|
49
|
+
This is a **Community** plugin. It is completely free and open-source for personal and commercial use under the MIT license.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { type TempoPlugin } from 'tempo-kit/plugin/sdk';
|
|
2
|
+
type SnapKey = 'hh' | 'hours' | 'hour' | 'mi' | 'minutes' | 'minute' | 'ss' | 'seconds' | 'second' | 'ms' | 'milliseconds' | 'millisecond' | 'us' | 'microseconds' | 'microsecond' | 'ns' | 'nanoseconds' | 'nanosecond';
|
|
3
|
+
/** Utility type that enforces exactly one key from the union */
|
|
4
|
+
type OneKey<K extends keyof any, V, KK extends keyof any = K> = {
|
|
5
|
+
[P in K]: {
|
|
6
|
+
[Q in P]: V;
|
|
7
|
+
} & {
|
|
8
|
+
[Q in Exclude<KK, P>]?: undefined;
|
|
9
|
+
} extends infer O ? {
|
|
10
|
+
[Q in keyof O]: O[Q];
|
|
11
|
+
} : never;
|
|
12
|
+
}[K];
|
|
13
|
+
/**
|
|
14
|
+
* Configuration options for snapping a Tempo instance.
|
|
15
|
+
* Accepts exactly one time component key mapped to a numeric step value,
|
|
16
|
+
* alongside an optional rounding direction.
|
|
17
|
+
*/
|
|
18
|
+
type SnapOptions = OneKey<SnapKey, number> & {
|
|
19
|
+
direction?: 'up' | 'down';
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* The Snap Plugin.
|
|
23
|
+
* Installs the `snap()` method onto Tempo instances, allowing time to be rounded to specific intervals.
|
|
24
|
+
*/
|
|
25
|
+
export declare const SnapPlugin: TempoPlugin;
|
|
26
|
+
declare module 'tempo-kit/core' {
|
|
27
|
+
interface Tempo {
|
|
28
|
+
/**
|
|
29
|
+
* Snaps the time to the nearest given interval for the specified unit.
|
|
30
|
+
*
|
|
31
|
+
* @param options - The snapping configuration including the time component, step value, and rounding direction.
|
|
32
|
+
* @returns A new snapped Tempo instance.
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* t.snap() // Snaps to nearest 15 minutes (default)
|
|
36
|
+
* t.snap({ hh: 1 }) // Snaps to nearest hour
|
|
37
|
+
* t.snap({ ss: 30 }) // Snaps to nearest 30 seconds
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
snap(options?: SnapOptions): Tempo;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var Magma;((Magma||={}).plugins||={}).snap=(()=>{var O=Object.create;var f=Object.defineProperty;var M=Object.getOwnPropertyDescriptor;var S=Object.getOwnPropertyNames;var w=Object.getPrototypeOf,P=Object.prototype.hasOwnProperty;var T=(s,e)=>()=>{try{return e||s((e={exports:{}}).exports,e),e.exports}catch(n){throw e=0,n}},b=(s,e)=>{for(var n in e)f(s,n,{get:e[n],enumerable:!0})},p=(s,e,n,a)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of S(e))!P.call(s,r)&&r!==n&&f(s,r,{get:()=>e[r],enumerable:!(a=M(e,r))||a.enumerable});return s},m=(s,e,n)=>(p(s,e,"default"),n&&p(n,e,"default")),k=(s,e,n)=>(n=s!=null?O(w(s)):{},p(e||!s||!s.__esModule?f(n,"default",{value:s,enumerable:!0}):n,s)),x=s=>p(f({},"__esModule",{value:!0}),s);var _=T((H,K)=>{"use strict";K.exports=window.Magma.pluginSdk});var Q={};b(Q,{SnapPlugin:()=>E});var c={};b(c,{definePlugin:()=>g,defineTerm:()=>v});m(c,k(_()));var l=k(_()),g=s=>(0,l.definePlugin)({...s,version:"2.0.0"}),v=s=>(0,l.defineTerm)({...s,version:"2.0.0"});var E=g({name:"snap",install(s){s.prototype.snap=function(e){let n=this,a=e&&Object.keys(e).length>0?e:{mi:15},r=a.direction,h=Object.keys(a).filter(o=>o!=="direction"&&a[o]!==void 0);if(h.length>1)return(0,c.logError)(new Error(`Snap options can only contain a single time component. Found: ${h.join(", ")}`),this.config),this;let y=h[0],d=a[y];if(!Number.isFinite(d)||d===0)return(0,c.logError)(new Error("Snap step cannot be zero or non-finite."),this.config),this;d=Math.abs(d);let u=(o,t,i)=>i==="up"?Math.ceil(o/t)*t:i==="down"?Math.floor(o/t)*t:Math.round(o/t)*t;switch(y){case"hh":case"hour":case"hours":{let o=n.hh+n.mi/60+n.ss/3600+n.ms/36e5+n.us/36e8+n.ns/36e11,i=u(o,d,r)-n.hh;n=n.set({mi:0,ss:0,ms:0,us:0,ns:0}),i!==0&&(n=n.add({hours:i}));break}case"mi":case"minute":case"minutes":{let o=n.mi+n.ss/60+n.ms/6e4+n.us/6e7+n.ns/6e10,i=u(o,d,r)-n.mi;n=n.set({ss:0,ms:0,us:0,ns:0}),i!==0&&(n=n.add({minutes:i}));break}case"ss":case"second":case"seconds":{let o=n.ss+n.ms/1e3+n.us/1e6+n.ns/1e9,i=u(o,d,r)-n.ss;n=n.set({ms:0,us:0,ns:0}),i!==0&&(n=n.add({seconds:i}));break}case"ms":case"millisecond":case"milliseconds":{let o=n.ms+n.us/1e3+n.ns/1e6,i=u(o,d,r)-n.ms;n=n.set({us:0,ns:0}),i!==0&&(n=n.add({milliseconds:i}));break}case"us":case"microsecond":case"microseconds":{let o=n.us+n.ns/1e3,i=u(o,d,r)-n.us;n=n.set({ns:0}),i!==0&&(n=n.add({microseconds:i}));break}case"ns":case"nanosecond":case"nanoseconds":{let t=u(n.ns,d,r)-n.ns;t!==0&&(n=n.add({nanoseconds:t}));break}default:return(0,c.logError)(new Error(`Invalid snap unit: '${y}'. Only time components (hours, minutes, seconds, milliseconds, microseconds, nanoseconds) are supported. Date components are not allowed.`),this.config),this}return n}}});return x(Q);})();
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
18
|
+
|
|
19
|
+
// auto-inject-version:tempo-kit/plugin/sdk
|
|
20
|
+
var sdk_exports = {};
|
|
21
|
+
__export(sdk_exports, {
|
|
22
|
+
definePlugin: () => definePlugin,
|
|
23
|
+
defineTerm: () => defineTerm
|
|
24
|
+
});
|
|
25
|
+
__reExport(sdk_exports, sdk_star);
|
|
26
|
+
import * as sdk_star from "tempo-kit/plugin/sdk";
|
|
27
|
+
import { definePlugin as originalPlugin, defineTerm as originalTerm } from "tempo-kit/plugin/sdk";
|
|
28
|
+
var definePlugin = (config) => originalPlugin({ ...config, version: "2.0.0" });
|
|
29
|
+
var defineTerm = (config) => originalTerm({ ...config, version: "2.0.0" });
|
|
30
|
+
|
|
31
|
+
// src/index.ts
|
|
32
|
+
var SnapPlugin = definePlugin({
|
|
33
|
+
name: "snap",
|
|
34
|
+
install(TempoClass) {
|
|
35
|
+
TempoClass.prototype.snap = function(options) {
|
|
36
|
+
let base = this;
|
|
37
|
+
const opts = options && Object.keys(options).length > 0 ? options : { mi: 15 };
|
|
38
|
+
const direction = opts.direction;
|
|
39
|
+
const providedKeys = Object.keys(opts).filter((k) => k !== "direction" && opts[k] !== void 0);
|
|
40
|
+
if (providedKeys.length > 1) {
|
|
41
|
+
(0, sdk_exports.logError)(new Error(`Snap options can only contain a single time component. Found: ${providedKeys.join(", ")}`), this.config);
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
const key = providedKeys[0];
|
|
45
|
+
let step = opts[key];
|
|
46
|
+
if (!Number.isFinite(step) || step === 0) {
|
|
47
|
+
(0, sdk_exports.logError)(new Error(`Snap step cannot be zero or non-finite.`), this.config);
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
step = Math.abs(step);
|
|
51
|
+
const calculateRounded = (fractional, stepSize, dir) => {
|
|
52
|
+
if (dir === "up") return Math.ceil(fractional / stepSize) * stepSize;
|
|
53
|
+
if (dir === "down") return Math.floor(fractional / stepSize) * stepSize;
|
|
54
|
+
return Math.round(fractional / stepSize) * stepSize;
|
|
55
|
+
};
|
|
56
|
+
switch (key) {
|
|
57
|
+
case "hh":
|
|
58
|
+
case "hour":
|
|
59
|
+
case "hours": {
|
|
60
|
+
const fractionalHour = base.hh + base.mi / 60 + base.ss / 3600 + base.ms / 36e5 + base.us / 36e8 + base.ns / 36e11;
|
|
61
|
+
const roundedHour = calculateRounded(fractionalHour, step, direction);
|
|
62
|
+
const diff = roundedHour - base.hh;
|
|
63
|
+
base = base.set({ mi: 0, ss: 0, ms: 0, us: 0, ns: 0 });
|
|
64
|
+
if (diff !== 0) base = base.add({ hours: diff });
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
case "mi":
|
|
68
|
+
case "minute":
|
|
69
|
+
case "minutes": {
|
|
70
|
+
const fractionalMinute = base.mi + base.ss / 60 + base.ms / 6e4 + base.us / 6e7 + base.ns / 6e10;
|
|
71
|
+
const roundedMinute = calculateRounded(fractionalMinute, step, direction);
|
|
72
|
+
const diff = roundedMinute - base.mi;
|
|
73
|
+
base = base.set({ ss: 0, ms: 0, us: 0, ns: 0 });
|
|
74
|
+
if (diff !== 0) base = base.add({ minutes: diff });
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
case "ss":
|
|
78
|
+
case "second":
|
|
79
|
+
case "seconds": {
|
|
80
|
+
const fractionalSecond = base.ss + base.ms / 1e3 + base.us / 1e6 + base.ns / 1e9;
|
|
81
|
+
const roundedSecond = calculateRounded(fractionalSecond, step, direction);
|
|
82
|
+
const diff = roundedSecond - base.ss;
|
|
83
|
+
base = base.set({ ms: 0, us: 0, ns: 0 });
|
|
84
|
+
if (diff !== 0) base = base.add({ seconds: diff });
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
case "ms":
|
|
88
|
+
case "millisecond":
|
|
89
|
+
case "milliseconds": {
|
|
90
|
+
const fractionalMs = base.ms + base.us / 1e3 + base.ns / 1e6;
|
|
91
|
+
const roundedMs = calculateRounded(fractionalMs, step, direction);
|
|
92
|
+
const diff = roundedMs - base.ms;
|
|
93
|
+
base = base.set({ us: 0, ns: 0 });
|
|
94
|
+
if (diff !== 0) base = base.add({ milliseconds: diff });
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
case "us":
|
|
98
|
+
case "microsecond":
|
|
99
|
+
case "microseconds": {
|
|
100
|
+
const fractionalUs = base.us + base.ns / 1e3;
|
|
101
|
+
const roundedUs = calculateRounded(fractionalUs, step, direction);
|
|
102
|
+
const diff = roundedUs - base.us;
|
|
103
|
+
base = base.set({ ns: 0 });
|
|
104
|
+
if (diff !== 0) base = base.add({ microseconds: diff });
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
case "ns":
|
|
108
|
+
case "nanosecond":
|
|
109
|
+
case "nanoseconds": {
|
|
110
|
+
const roundedNs = calculateRounded(base.ns, step, direction);
|
|
111
|
+
const diff = roundedNs - base.ns;
|
|
112
|
+
if (diff !== 0) base = base.add({ nanoseconds: diff });
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
default: {
|
|
116
|
+
(0, sdk_exports.logError)(new Error(`Invalid snap unit: '${key}'. Only time components (hours, minutes, seconds, milliseconds, microseconds, nanoseconds) are supported. Date components are not allowed.`), this.config);
|
|
117
|
+
return this;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return base;
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
export {
|
|
125
|
+
SnapPlugin
|
|
126
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './index.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var Magma;((Magma||={}).plugins||={}).snap=(()=>{var S=Object.create;var f=Object.defineProperty;var M=Object.getOwnPropertyDescriptor;var P=Object.getOwnPropertyNames;var w=Object.getPrototypeOf,x=Object.prototype.hasOwnProperty;var T=(s,e)=>()=>{try{return e||s((e={exports:{}}).exports,e),e.exports}catch(n){throw e=0,n}},k=(s,e)=>{for(var n in e)f(s,n,{get:e[n],enumerable:!0})},p=(s,e,n,a)=>{if(e&&typeof e=="object"||typeof e=="function")for(let c of P(e))!x.call(s,c)&&c!==n&&f(s,c,{get:()=>e[c],enumerable:!(a=M(e,c))||a.enumerable});return s},m=(s,e,n)=>(p(s,e,"default"),n&&p(n,e,"default")),K=(s,e,n)=>(n=s!=null?S(w(s)):{},p(e||!s||!s.__esModule?f(n,"default",{value:s,enumerable:!0}):n,s)),v=s=>p(f({},"__esModule",{value:!0}),s);var _=T((H,O)=>{"use strict";O.exports=window.Magma.pluginSdk});var Q={};k(Q,{SnapPlugin:()=>b});var r={};k(r,{definePlugin:()=>g,defineTerm:()=>E});m(r,K(_()));var l=K(_()),g=s=>(0,l.definePlugin)({...s,version:"2.0.0"}),E=s=>(0,l.defineTerm)({...s,version:"2.0.0"});var b=g({name:"snap",install(s){s.prototype.snap=function(e){let n=this,a=e&&Object.keys(e).length>0?e:{mi:15},c=a.direction,h=Object.keys(a).filter(o=>o!=="direction"&&a[o]!==void 0);if(h.length>1)return(0,r.logError)(new Error(`Snap options can only contain a single time component. Found: ${h.join(", ")}`),this.config),this;let y=h[0],d=a[y];if(!Number.isFinite(d)||d===0)return(0,r.logError)(new Error("Snap step cannot be zero or non-finite."),this.config),this;d=Math.abs(d);let u=(o,t,i)=>i==="up"?Math.ceil(o/t)*t:i==="down"?Math.floor(o/t)*t:Math.round(o/t)*t;switch(y){case"hh":case"hour":case"hours":{let o=n.hh+n.mi/60+n.ss/3600+n.ms/36e5+n.us/36e8+n.ns/36e11,i=u(o,d,c)-n.hh;n=n.set({mi:0,ss:0,ms:0,us:0,ns:0}),i!==0&&(n=n.add({hours:i}));break}case"mi":case"minute":case"minutes":{let o=n.mi+n.ss/60+n.ms/6e4+n.us/6e7+n.ns/6e10,i=u(o,d,c)-n.mi;n=n.set({ss:0,ms:0,us:0,ns:0}),i!==0&&(n=n.add({minutes:i}));break}case"ss":case"second":case"seconds":{let o=n.ss+n.ms/1e3+n.us/1e6+n.ns/1e9,i=u(o,d,c)-n.ss;n=n.set({ms:0,us:0,ns:0}),i!==0&&(n=n.add({seconds:i}));break}case"ms":case"millisecond":case"milliseconds":{let o=n.ms+n.us/1e3+n.ns/1e6,i=u(o,d,c)-n.ms;n=n.set({us:0,ns:0}),i!==0&&(n=n.add({milliseconds:i}));break}case"us":case"microsecond":case"microseconds":{let o=n.us+n.ns/1e3,i=u(o,d,c)-n.us;n=n.set({ns:0}),i!==0&&(n=n.add({microseconds:i}));break}case"ns":case"nanosecond":case"nanoseconds":{let t=u(n.ns,d,c)-n.ns;t!==0&&(n=n.add({nanoseconds:t}));break}default:return(0,r.logError)(new Error(`Invalid snap unit: '${y}'. Only time components (hours, minutes, seconds, milliseconds, microseconds, nanoseconds) are supported. Date components are not allowed.`),this.config),this}return n}}});(0,r.autoInstall)(b);return v(Q);})();
|
package/dist/install.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
18
|
+
|
|
19
|
+
// auto-inject-version:tempo-kit/plugin/sdk
|
|
20
|
+
var sdk_exports = {};
|
|
21
|
+
__export(sdk_exports, {
|
|
22
|
+
definePlugin: () => definePlugin,
|
|
23
|
+
defineTerm: () => defineTerm
|
|
24
|
+
});
|
|
25
|
+
__reExport(sdk_exports, sdk_star);
|
|
26
|
+
import * as sdk_star from "tempo-kit/plugin/sdk";
|
|
27
|
+
import { definePlugin as originalPlugin, defineTerm as originalTerm } from "tempo-kit/plugin/sdk";
|
|
28
|
+
var definePlugin = (config) => originalPlugin({ ...config, version: "2.0.0" });
|
|
29
|
+
var defineTerm = (config) => originalTerm({ ...config, version: "2.0.0" });
|
|
30
|
+
|
|
31
|
+
// src/index.ts
|
|
32
|
+
var SnapPlugin = definePlugin({
|
|
33
|
+
name: "snap",
|
|
34
|
+
install(TempoClass) {
|
|
35
|
+
TempoClass.prototype.snap = function(options) {
|
|
36
|
+
let base = this;
|
|
37
|
+
const opts = options && Object.keys(options).length > 0 ? options : { mi: 15 };
|
|
38
|
+
const direction = opts.direction;
|
|
39
|
+
const providedKeys = Object.keys(opts).filter((k) => k !== "direction" && opts[k] !== void 0);
|
|
40
|
+
if (providedKeys.length > 1) {
|
|
41
|
+
(0, sdk_exports.logError)(new Error(`Snap options can only contain a single time component. Found: ${providedKeys.join(", ")}`), this.config);
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
const key = providedKeys[0];
|
|
45
|
+
let step = opts[key];
|
|
46
|
+
if (!Number.isFinite(step) || step === 0) {
|
|
47
|
+
(0, sdk_exports.logError)(new Error(`Snap step cannot be zero or non-finite.`), this.config);
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
step = Math.abs(step);
|
|
51
|
+
const calculateRounded = (fractional, stepSize, dir) => {
|
|
52
|
+
if (dir === "up") return Math.ceil(fractional / stepSize) * stepSize;
|
|
53
|
+
if (dir === "down") return Math.floor(fractional / stepSize) * stepSize;
|
|
54
|
+
return Math.round(fractional / stepSize) * stepSize;
|
|
55
|
+
};
|
|
56
|
+
switch (key) {
|
|
57
|
+
case "hh":
|
|
58
|
+
case "hour":
|
|
59
|
+
case "hours": {
|
|
60
|
+
const fractionalHour = base.hh + base.mi / 60 + base.ss / 3600 + base.ms / 36e5 + base.us / 36e8 + base.ns / 36e11;
|
|
61
|
+
const roundedHour = calculateRounded(fractionalHour, step, direction);
|
|
62
|
+
const diff = roundedHour - base.hh;
|
|
63
|
+
base = base.set({ mi: 0, ss: 0, ms: 0, us: 0, ns: 0 });
|
|
64
|
+
if (diff !== 0) base = base.add({ hours: diff });
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
case "mi":
|
|
68
|
+
case "minute":
|
|
69
|
+
case "minutes": {
|
|
70
|
+
const fractionalMinute = base.mi + base.ss / 60 + base.ms / 6e4 + base.us / 6e7 + base.ns / 6e10;
|
|
71
|
+
const roundedMinute = calculateRounded(fractionalMinute, step, direction);
|
|
72
|
+
const diff = roundedMinute - base.mi;
|
|
73
|
+
base = base.set({ ss: 0, ms: 0, us: 0, ns: 0 });
|
|
74
|
+
if (diff !== 0) base = base.add({ minutes: diff });
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
case "ss":
|
|
78
|
+
case "second":
|
|
79
|
+
case "seconds": {
|
|
80
|
+
const fractionalSecond = base.ss + base.ms / 1e3 + base.us / 1e6 + base.ns / 1e9;
|
|
81
|
+
const roundedSecond = calculateRounded(fractionalSecond, step, direction);
|
|
82
|
+
const diff = roundedSecond - base.ss;
|
|
83
|
+
base = base.set({ ms: 0, us: 0, ns: 0 });
|
|
84
|
+
if (diff !== 0) base = base.add({ seconds: diff });
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
case "ms":
|
|
88
|
+
case "millisecond":
|
|
89
|
+
case "milliseconds": {
|
|
90
|
+
const fractionalMs = base.ms + base.us / 1e3 + base.ns / 1e6;
|
|
91
|
+
const roundedMs = calculateRounded(fractionalMs, step, direction);
|
|
92
|
+
const diff = roundedMs - base.ms;
|
|
93
|
+
base = base.set({ us: 0, ns: 0 });
|
|
94
|
+
if (diff !== 0) base = base.add({ milliseconds: diff });
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
case "us":
|
|
98
|
+
case "microsecond":
|
|
99
|
+
case "microseconds": {
|
|
100
|
+
const fractionalUs = base.us + base.ns / 1e3;
|
|
101
|
+
const roundedUs = calculateRounded(fractionalUs, step, direction);
|
|
102
|
+
const diff = roundedUs - base.us;
|
|
103
|
+
base = base.set({ ns: 0 });
|
|
104
|
+
if (diff !== 0) base = base.add({ microseconds: diff });
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
case "ns":
|
|
108
|
+
case "nanosecond":
|
|
109
|
+
case "nanoseconds": {
|
|
110
|
+
const roundedNs = calculateRounded(base.ns, step, direction);
|
|
111
|
+
const diff = roundedNs - base.ns;
|
|
112
|
+
if (diff !== 0) base = base.add({ nanoseconds: diff });
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
default: {
|
|
116
|
+
(0, sdk_exports.logError)(new Error(`Invalid snap unit: '${key}'. Only time components (hours, minutes, seconds, milliseconds, microseconds, nanoseconds) are supported. Date components are not allowed.`), this.config);
|
|
117
|
+
return this;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return base;
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
// src/install.ts
|
|
126
|
+
(0, sdk_exports.autoInstall)(SnapPlugin);
|
|
127
|
+
export {
|
|
128
|
+
SnapPlugin
|
|
129
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,62 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tempo-kit/plugin-snap",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "Tempo plugin for rounding and snapping dates to custom minute, hour, or sub-second intervals — ideal for calendar UIs, time-pickers, and telemetry aggregation",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"sideEffects": [
|
|
8
|
+
"./dist/install.js"
|
|
9
|
+
],
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/magmacomputing/magma.git",
|
|
14
|
+
"directory": "packages/plugins/snap"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"README.md",
|
|
19
|
+
"CHANGELOG.md",
|
|
20
|
+
"LICENSE"
|
|
21
|
+
],
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"registry": "https://registry.npmjs.org/",
|
|
24
|
+
"access": "public"
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsup && tsc",
|
|
28
|
+
"test": "vitest run -c ../vitest.shared.ts",
|
|
29
|
+
"prepublishOnly": "tempo-cli prepublish"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"tempo-kit": "^5.0.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {},
|
|
35
|
+
"keywords": [
|
|
36
|
+
"tempo-kit",
|
|
37
|
+
"tempo-kit-plugin",
|
|
38
|
+
"tempo",
|
|
39
|
+
"temporal",
|
|
40
|
+
"tempo-plugin",
|
|
41
|
+
"plugin",
|
|
42
|
+
"snap",
|
|
43
|
+
"round",
|
|
44
|
+
"date-time",
|
|
45
|
+
"scheduling",
|
|
46
|
+
"calendar",
|
|
47
|
+
"typescript"
|
|
48
|
+
],
|
|
49
|
+
"type": "module",
|
|
50
|
+
"exports": {
|
|
51
|
+
".": {
|
|
52
|
+
"types": "./dist/index.d.ts",
|
|
53
|
+
"import": "./dist/index.js",
|
|
54
|
+
"default": "./dist/index.js"
|
|
55
|
+
},
|
|
56
|
+
"./install": {
|
|
57
|
+
"types": "./dist/index.d.ts",
|
|
58
|
+
"import": "./dist/install.js",
|
|
59
|
+
"default": "./dist/install.js"
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|