@duplojs/utils 1.5.6 → 1.5.8
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/clean/entity/index.cjs +94 -0
- package/dist/clean/entity/index.d.ts +217 -0
- package/dist/clean/entity/index.mjs +85 -0
- package/dist/clean/entity/property.cjs +66 -0
- package/dist/clean/entity/property.d.ts +43 -0
- package/dist/clean/entity/property.mjs +59 -0
- package/dist/clean/entity/unwrap.cjs +58 -0
- package/dist/clean/{unwrapEntity.d.ts → entity/unwrap.d.ts} +13 -10
- package/dist/clean/entity/unwrap.mjs +55 -0
- package/dist/clean/index.cjs +14 -6
- package/dist/clean/index.d.ts +0 -1
- package/dist/clean/index.mjs +3 -2
- package/dist/clean/maybe.cjs +3 -3
- package/dist/clean/maybe.mjs +1 -1
- package/dist/dataParser/extended/tuple.d.ts +6 -2
- package/dist/date/computeTime.cjs +23 -0
- package/dist/date/computeTime.d.ts +41 -0
- package/dist/date/computeTime.mjs +21 -0
- package/dist/date/getDifference.cjs +17 -0
- package/dist/date/getDifference.d.ts +44 -0
- package/dist/date/getDifference.mjs +15 -0
- package/dist/date/index.cjs +4 -0
- package/dist/date/index.d.ts +2 -0
- package/dist/date/index.mjs +2 -0
- package/dist/date/theTime.d.ts +3 -2
- package/dist/date/toNative.cjs +11 -5
- package/dist/date/toNative.d.ts +21 -7
- package/dist/date/toNative.mjs +11 -5
- package/dist/date/types/serializedTheTime.d.ts +2 -1
- package/dist/metadata.json +50 -18
- package/package.json +2 -2
- package/dist/clean/entity.cjs +0 -145
- package/dist/clean/entity.d.ts +0 -234
- package/dist/clean/entity.mjs +0 -141
- package/dist/clean/unwrapEntity.cjs +0 -36
- package/dist/clean/unwrapEntity.mjs +0 -34
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
import { entityKind, type Entity } from "
|
|
3
|
-
import { flagKind } from "
|
|
4
|
-
type
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import { type Unwrap, type WrappedValue, type IsEqual, type Transformer, type TransformerFunction, type SimplifyTopLevel, type DeepReadonly, type GetKindValue, type Kind } from "../../common";
|
|
2
|
+
import { entityKind, type Entity } from ".";
|
|
3
|
+
import { flagKind } from "../flag";
|
|
4
|
+
export type UnwrapEntityProperty<GenericProperty extends unknown, GenericTransformer extends TransformerFunction = never> = GenericProperty extends WrappedValue ? IsEqual<GenericTransformer, never> extends true ? Unwrap<GenericProperty> : GenericTransformer extends TransformerFunction<infer InferredMethodName> ? Transformer<Unwrap<GenericProperty>, InferredMethodName> : never : GenericProperty extends null ? null : GenericProperty extends readonly [infer InferredFirst, ...infer InferredRest] ? readonly [
|
|
5
|
+
UnwrapEntityProperty<InferredFirst, GenericTransformer>,
|
|
6
|
+
...UnwrapEntityProperty<InferredRest, GenericTransformer>
|
|
7
|
+
] : GenericProperty extends readonly [] ? readonly [] : GenericProperty extends readonly unknown[] ? readonly UnwrapEntityProperty<GenericProperty[number], GenericTransformer>[] : GenericProperty extends Record<string, unknown> ? {
|
|
8
|
+
[Prop in keyof GenericProperty]: UnwrapEntityProperty<GenericProperty[Prop], GenericTransformer>;
|
|
9
|
+
} : GenericProperty;
|
|
9
10
|
export type UnwrapEntity<GenericEntity extends Entity, GenericTransformer extends TransformerFunction = never> = SimplifyTopLevel<DeepReadonly<{
|
|
10
|
-
[Prop in Extract<keyof GenericEntity, string>]:
|
|
11
|
+
[Prop in Extract<keyof GenericEntity, string>]: UnwrapEntityProperty<GenericEntity[Prop], GenericTransformer>;
|
|
11
12
|
} & {
|
|
12
13
|
[Prop in "_entityName"]: GetKindValue<typeof entityKind, GenericEntity>;
|
|
13
14
|
} & (GenericEntity extends Kind<typeof flagKind.definition, any> ? {
|
|
@@ -59,7 +60,9 @@ export type UnwrapEntity<GenericEntity extends Entity, GenericTransformer extend
|
|
|
59
60
|
* @namespace C
|
|
60
61
|
*
|
|
61
62
|
*/
|
|
63
|
+
export declare function unwrapEntityProperty<GenericProperty extends unknown, GenericTransformer extends TransformerFunction = never>(property: GenericProperty, params?: {
|
|
64
|
+
transformer?: GenericTransformer;
|
|
65
|
+
}): UnwrapEntityProperty<GenericProperty, GenericTransformer>;
|
|
62
66
|
export declare function unwrapEntity<GenericEntity extends Entity, GenericTransformer extends TransformerFunction = never>(entity: GenericEntity, params?: {
|
|
63
67
|
transformer?: GenericTransformer;
|
|
64
68
|
}): UnwrapEntity<GenericEntity, GenericTransformer>;
|
|
65
|
-
export {};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { entityKind } from './index.mjs';
|
|
2
|
+
import { flagKind } from '../flag.mjs';
|
|
3
|
+
import { isWrappedValue } from '../../common/wrapValue.mjs';
|
|
4
|
+
import { unwrap } from '../../common/unwrap.mjs';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* {@include clean/unwrapEntity/index.md}
|
|
8
|
+
*/
|
|
9
|
+
function unwrapEntityProperty(property, params) {
|
|
10
|
+
if (isWrappedValue(property)) {
|
|
11
|
+
return params?.transformer
|
|
12
|
+
? params.transformer(unwrap(property))
|
|
13
|
+
: unwrap(property);
|
|
14
|
+
}
|
|
15
|
+
else if (property === null) {
|
|
16
|
+
return property;
|
|
17
|
+
}
|
|
18
|
+
else if (property instanceof Array) {
|
|
19
|
+
const length = property.length;
|
|
20
|
+
const result = [];
|
|
21
|
+
for (let index = 0; index < length; index++) {
|
|
22
|
+
result[index] = unwrapEntityProperty(property[index], params);
|
|
23
|
+
}
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
26
|
+
else if (typeof property === "object"
|
|
27
|
+
&& (!property.constructor
|
|
28
|
+
|| property.constructor.name === "Object")) {
|
|
29
|
+
const result = {};
|
|
30
|
+
for (const key in property) {
|
|
31
|
+
result[key] = unwrapEntityProperty(property[key], params);
|
|
32
|
+
}
|
|
33
|
+
return result;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
return property;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function unwrapEntity(entity, params) {
|
|
40
|
+
const unwrapEntity = {};
|
|
41
|
+
for (const prop in entity) {
|
|
42
|
+
if (prop === entityKind.runTimeKey) {
|
|
43
|
+
unwrapEntity._entityName = entity[prop];
|
|
44
|
+
}
|
|
45
|
+
else if (prop === flagKind.runTimeKey) {
|
|
46
|
+
unwrapEntity._flags = entity[prop];
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
unwrapEntity[prop] = unwrapEntityProperty(entity[prop], params);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return unwrapEntity;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export { unwrapEntity, unwrapEntityProperty };
|
package/dist/clean/index.cjs
CHANGED
|
@@ -2,12 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
var kind = require('./kind.cjs');
|
|
4
4
|
var newType = require('./newType.cjs');
|
|
5
|
-
var
|
|
5
|
+
var index = require('./entity/index.cjs');
|
|
6
6
|
var repository = require('./repository.cjs');
|
|
7
7
|
var useCase = require('./useCase.cjs');
|
|
8
8
|
var flag = require('./flag.cjs');
|
|
9
|
-
var unwrapEntity = require('./unwrapEntity.cjs');
|
|
10
9
|
var maybe = require('./maybe.cjs');
|
|
10
|
+
var property = require('./entity/property.cjs');
|
|
11
|
+
var unwrap = require('./entity/unwrap.cjs');
|
|
11
12
|
var base = require('./constraint/base.cjs');
|
|
12
13
|
var number = require('./constraint/defaultConstraint/number.cjs');
|
|
13
14
|
var string = require('./constraint/defaultConstraint/string.cjs');
|
|
@@ -49,9 +50,9 @@ exports.CreateNewTypeError = newType.CreateNewTypeError;
|
|
|
49
50
|
exports.createNewType = newType.createNewType;
|
|
50
51
|
exports.newTypeHandlerKind = newType.newTypeHandlerKind;
|
|
51
52
|
exports.newTypeKind = newType.newTypeKind;
|
|
52
|
-
exports.CreateEntityError =
|
|
53
|
-
exports.createEntity =
|
|
54
|
-
exports.entityKind =
|
|
53
|
+
exports.CreateEntityError = index.CreateEntityError;
|
|
54
|
+
exports.createEntity = index.createEntity;
|
|
55
|
+
exports.entityKind = index.entityKind;
|
|
55
56
|
exports.createRepository = repository.createRepository;
|
|
56
57
|
exports.repositoryHandlerKind = repository.repositoryHandlerKind;
|
|
57
58
|
exports.createUseCase = useCase.createUseCase;
|
|
@@ -59,9 +60,16 @@ exports.useCaseHandlerKind = useCase.useCaseHandlerKind;
|
|
|
59
60
|
exports.useCaseInstances = useCase.useCaseInstances;
|
|
60
61
|
exports.createFlag = flag.createFlag;
|
|
61
62
|
exports.flagKind = flag.flagKind;
|
|
62
|
-
exports.unwrapEntity = unwrapEntity.unwrapEntity;
|
|
63
63
|
exports.none = maybe.none;
|
|
64
64
|
exports.some = maybe.some;
|
|
65
|
+
exports.entityPropertyArrayKind = property.entityPropertyArrayKind;
|
|
66
|
+
exports.entityPropertyDefinitionToDataParser = property.entityPropertyDefinitionToDataParser;
|
|
67
|
+
exports.entityPropertyDefinitionTools = property.entityPropertyDefinitionTools;
|
|
68
|
+
exports.entityPropertyNullableKind = property.entityPropertyNullableKind;
|
|
69
|
+
exports.entityPropertyStructureKind = property.entityPropertyStructureKind;
|
|
70
|
+
exports.entityPropertyUnionKind = property.entityPropertyUnionKind;
|
|
71
|
+
exports.unwrapEntity = unwrap.unwrapEntity;
|
|
72
|
+
exports.unwrapEntityProperty = unwrap.unwrapEntityProperty;
|
|
65
73
|
exports.CreateConstrainedTypeError = base.CreateConstrainedTypeError;
|
|
66
74
|
exports.constrainedTypeKind = base.constrainedTypeKind;
|
|
67
75
|
exports.constraintHandlerKind = base.constraintHandlerKind;
|
package/dist/clean/index.d.ts
CHANGED
package/dist/clean/index.mjs
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
export { createCleanKind } from './kind.mjs';
|
|
2
2
|
export { CreateNewTypeError, createNewType, newTypeHandlerKind, newTypeKind } from './newType.mjs';
|
|
3
|
-
export { CreateEntityError, createEntity, entityKind } from './entity.mjs';
|
|
3
|
+
export { CreateEntityError, createEntity, entityKind } from './entity/index.mjs';
|
|
4
4
|
export { createRepository, repositoryHandlerKind } from './repository.mjs';
|
|
5
5
|
export { createUseCase, useCaseHandlerKind, useCaseInstances } from './useCase.mjs';
|
|
6
6
|
export { createFlag, flagKind } from './flag.mjs';
|
|
7
|
-
export { unwrapEntity } from './unwrapEntity.mjs';
|
|
8
7
|
export { none, some } from './maybe.mjs';
|
|
8
|
+
export { entityPropertyArrayKind, entityPropertyDefinitionToDataParser, entityPropertyDefinitionTools, entityPropertyNullableKind, entityPropertyStructureKind, entityPropertyUnionKind } from './entity/property.mjs';
|
|
9
|
+
export { unwrapEntity, unwrapEntityProperty } from './entity/unwrap.mjs';
|
|
9
10
|
export { CreateConstrainedTypeError, constrainedTypeKind, constraintHandlerKind, createConstraint } from './constraint/base.mjs';
|
|
10
11
|
export { Int, Negative, NumberMax, NumberMin, Positive, PositiveInt } from './constraint/defaultConstraint/number.mjs';
|
|
11
12
|
export { Email, StringMax, StringMin, Url } from './constraint/defaultConstraint/string.mjs';
|
package/dist/clean/maybe.cjs
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var index = require('./entity/index.cjs');
|
|
4
4
|
var create = require('../either/right/create.cjs');
|
|
5
5
|
var create$1 = require('../either/left/create.cjs');
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* {@include clean/some/index.md}
|
|
9
9
|
*/
|
|
10
|
-
function some(entity
|
|
11
|
-
return create.right(`some-${
|
|
10
|
+
function some(entity) {
|
|
11
|
+
return create.right(`some-${index.entityKind.getValue(entity)}`, entity);
|
|
12
12
|
}
|
|
13
13
|
/**
|
|
14
14
|
* {@include clean/none/index.md}
|
package/dist/clean/maybe.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type FixDeepFunctionInfer, type Kind, type NeverCoalescing, type SimplifyTopLevel } from "../../common";
|
|
1
|
+
import { type FixDeepFunctionInfer, type IsEqual, type Kind, type NeverCoalescing, type Or, type SimplifyTopLevel } from "../../common";
|
|
2
2
|
import { type DataParserExtended } from "../baseExtended";
|
|
3
3
|
import { type AddCheckersToDefinition, type MergeDefinition, type DataParsers } from "../types";
|
|
4
4
|
import * as dataParsers from "../parsers";
|
|
@@ -139,7 +139,11 @@ export interface DataParserTupleExtended<GenericDefinition extends dataParsers.D
|
|
|
139
139
|
*
|
|
140
140
|
*/
|
|
141
141
|
export declare function tuple<GenericShape extends dataParsers.TupleShape, const GenericDefinition extends Partial<Omit<dataParsers.DataParserDefinitionTuple, "shape">> = never>(shape: GenericShape, definition?: GenericDefinition): DataParserTupleExtended<MergeDefinition<dataParsers.DataParserDefinitionTuple, NeverCoalescing<GenericDefinition, {}> & {
|
|
142
|
-
shape: GenericShape;
|
|
142
|
+
readonly shape: GenericShape;
|
|
143
|
+
readonly rest: Or<[
|
|
144
|
+
IsEqual<GenericDefinition["rest"], unknown>,
|
|
145
|
+
IsEqual<GenericDefinition, never>
|
|
146
|
+
]> extends true ? undefined : GenericDefinition["rest"];
|
|
143
147
|
}>>;
|
|
144
148
|
export declare namespace tuple {
|
|
145
149
|
var overrideHandler: import("../../common").OverrideHandler<DataParserTupleExtended<dataParsers.DataParserDefinitionTuple>>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var constants = require('./constants.cjs');
|
|
4
|
+
var toTimeValue = require('./toTimeValue.cjs');
|
|
5
|
+
|
|
6
|
+
const unitMapper = {
|
|
7
|
+
week: 1 / constants.millisecondInOneWeek,
|
|
8
|
+
day: 1 / constants.millisecondsInOneDay,
|
|
9
|
+
hour: 1 / constants.millisecondInOneHour,
|
|
10
|
+
minute: 1 / constants.millisecondInOneMinute,
|
|
11
|
+
second: 1 / constants.millisecondsInOneSecond,
|
|
12
|
+
millisecond: 1,
|
|
13
|
+
};
|
|
14
|
+
function computeTime(...args) {
|
|
15
|
+
if (args.length === 1) {
|
|
16
|
+
const [unit] = args;
|
|
17
|
+
return (input) => computeTime(input, unit);
|
|
18
|
+
}
|
|
19
|
+
const [input, unit] = args;
|
|
20
|
+
return toTimeValue.toTimeValue(input) * unitMapper[unit];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
exports.computeTime = computeTime;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { TheTime } from "./theTime";
|
|
2
|
+
import type { SerializedTheTime } from "./types";
|
|
3
|
+
type ComputeTimeUnit = "week" | "day" | "hour" | "minute" | "second" | "millisecond";
|
|
4
|
+
/**
|
|
5
|
+
* Converts a time value to a numeric value in the requested time unit.
|
|
6
|
+
*
|
|
7
|
+
* **Supported call styles:**
|
|
8
|
+
* - Classic: `computeTime(input, unit)` → `number`
|
|
9
|
+
* - Curried: `computeTime(unit)` → `(input) => number`
|
|
10
|
+
*
|
|
11
|
+
* `input` accepts `TheTime` or `SerializedTheTime`.
|
|
12
|
+
* `unit` accepts `week | day | hour | minute | second | millisecond`.
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* const input = D.createTime(7_200_000, "millisecond");
|
|
16
|
+
*
|
|
17
|
+
* const hours = D.computeTime(input, "hour");
|
|
18
|
+
* // hours: number
|
|
19
|
+
*
|
|
20
|
+
* const minutesFromSerialized = D.computeTime("time3600000-", "minute");
|
|
21
|
+
* // minutesFromSerialized: number
|
|
22
|
+
*
|
|
23
|
+
* const days = pipe(
|
|
24
|
+
* D.createTime(172_800_000, "millisecond"),
|
|
25
|
+
* D.computeTime("day"),
|
|
26
|
+
* );
|
|
27
|
+
* // days: number
|
|
28
|
+
*
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @remarks
|
|
32
|
+
* - `computeTime` keeps the sign of the input value.
|
|
33
|
+
*
|
|
34
|
+
* @see https://utils.duplojs.dev/en/v1/api/date/computeTime
|
|
35
|
+
*
|
|
36
|
+
* @namespace D
|
|
37
|
+
*
|
|
38
|
+
*/
|
|
39
|
+
export declare function computeTime(unit: ComputeTimeUnit): (input: TheTime | SerializedTheTime) => number;
|
|
40
|
+
export declare function computeTime(input: TheTime | SerializedTheTime, unit: ComputeTimeUnit): number;
|
|
41
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { millisecondsInOneSecond, millisecondInOneMinute, millisecondInOneHour, millisecondsInOneDay, millisecondInOneWeek } from './constants.mjs';
|
|
2
|
+
import { toTimeValue } from './toTimeValue.mjs';
|
|
3
|
+
|
|
4
|
+
const unitMapper = {
|
|
5
|
+
week: 1 / millisecondInOneWeek,
|
|
6
|
+
day: 1 / millisecondsInOneDay,
|
|
7
|
+
hour: 1 / millisecondInOneHour,
|
|
8
|
+
minute: 1 / millisecondInOneMinute,
|
|
9
|
+
second: 1 / millisecondsInOneSecond,
|
|
10
|
+
millisecond: 1,
|
|
11
|
+
};
|
|
12
|
+
function computeTime(...args) {
|
|
13
|
+
if (args.length === 1) {
|
|
14
|
+
const [unit] = args;
|
|
15
|
+
return (input) => computeTime(input, unit);
|
|
16
|
+
}
|
|
17
|
+
const [input, unit] = args;
|
|
18
|
+
return toTimeValue(input) * unitMapper[unit];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export { computeTime };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var makeSafeTimeValue = require('./makeSafeTimeValue.cjs');
|
|
4
|
+
var theTime = require('./theTime.cjs');
|
|
5
|
+
var toTimestamp = require('./toTimestamp.cjs');
|
|
6
|
+
|
|
7
|
+
function getDifference(...args) {
|
|
8
|
+
if (args.length === 1) {
|
|
9
|
+
const [reference] = args;
|
|
10
|
+
return (input) => getDifference(input, reference);
|
|
11
|
+
}
|
|
12
|
+
const inputTimestamp = toTimestamp.toTimestamp(args[0]);
|
|
13
|
+
const referenceTimestamp = toTimestamp.toTimestamp(args[1]);
|
|
14
|
+
return theTime.TheTime.new(makeSafeTimeValue.makeSafeTimeValue(inputTimestamp - referenceTimestamp));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
exports.getDifference = getDifference;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { TheDate } from "./theDate";
|
|
2
|
+
import { TheTime } from "./theTime";
|
|
3
|
+
import type { SerializedTheDate } from "./types";
|
|
4
|
+
/**
|
|
5
|
+
* Returns the time difference between two dates as `TheTime`.
|
|
6
|
+
*
|
|
7
|
+
* **Supported call styles:**
|
|
8
|
+
* - Classic: `getDifference(input, reference)` → `TheTime`
|
|
9
|
+
* - Curried: `getDifference(reference)` → `(input) => TheTime`
|
|
10
|
+
*
|
|
11
|
+
* The difference is computed as `input - reference` in milliseconds and returned as `TheTime`.
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* const input = D.create("2024-01-03");
|
|
15
|
+
* const reference = D.create("2024-01-01");
|
|
16
|
+
*
|
|
17
|
+
* const difference = D.getDifference(input, reference);
|
|
18
|
+
* // difference: TheTime
|
|
19
|
+
*
|
|
20
|
+
* const asMilliseconds = difference.toNative();
|
|
21
|
+
* // asMilliseconds: number
|
|
22
|
+
*
|
|
23
|
+
* const withSerialized = D.getDifference(
|
|
24
|
+
* "date172800000+",
|
|
25
|
+
* "date86400000+",
|
|
26
|
+
* );
|
|
27
|
+
* // withSerialized: TheTime
|
|
28
|
+
*
|
|
29
|
+
* pipe(
|
|
30
|
+
* input,
|
|
31
|
+
* D.getDifference(reference),
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @remarks
|
|
35
|
+
* - `TheTime` supports negative values, so `getDifference` preserves the sign of `input - reference`.
|
|
36
|
+
* - Input accepts `TheDate` and `SerializedTheDate`.
|
|
37
|
+
*
|
|
38
|
+
* @see https://utils.duplojs.dev/en/v1/api/date/getDifference
|
|
39
|
+
*
|
|
40
|
+
* @namespace D
|
|
41
|
+
*
|
|
42
|
+
*/
|
|
43
|
+
export declare function getDifference(reference: TheDate | SerializedTheDate): (input: TheDate | SerializedTheDate) => TheTime;
|
|
44
|
+
export declare function getDifference(input: TheDate | SerializedTheDate, reference: TheDate | SerializedTheDate): TheTime;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { makeSafeTimeValue } from './makeSafeTimeValue.mjs';
|
|
2
|
+
import { TheTime } from './theTime.mjs';
|
|
3
|
+
import { toTimestamp } from './toTimestamp.mjs';
|
|
4
|
+
|
|
5
|
+
function getDifference(...args) {
|
|
6
|
+
if (args.length === 1) {
|
|
7
|
+
const [reference] = args;
|
|
8
|
+
return (input) => getDifference(input, reference);
|
|
9
|
+
}
|
|
10
|
+
const inputTimestamp = toTimestamp(args[0]);
|
|
11
|
+
const referenceTimestamp = toTimestamp(args[1]);
|
|
12
|
+
return TheTime.new(makeSafeTimeValue(inputTimestamp - referenceTimestamp));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { getDifference };
|
package/dist/date/index.cjs
CHANGED
|
@@ -9,6 +9,7 @@ var tomorrow = require('./tomorrow.cjs');
|
|
|
9
9
|
var toNative = require('./toNative.cjs');
|
|
10
10
|
var toTimestamp = require('./toTimestamp.cjs');
|
|
11
11
|
var toTimeValue = require('./toTimeValue.cjs');
|
|
12
|
+
var computeTime = require('./computeTime.cjs');
|
|
12
13
|
var toISOString = require('./toISOString.cjs');
|
|
13
14
|
var closestTo = require('./closestTo.cjs');
|
|
14
15
|
var each = require('./each.cjs');
|
|
@@ -29,6 +30,7 @@ var getTimezoneOffset = require('./getTimezoneOffset.cjs');
|
|
|
29
30
|
var applyTimezone = require('./applyTimezone.cjs');
|
|
30
31
|
var format = require('./format.cjs');
|
|
31
32
|
var formatTime = require('./formatTime.cjs');
|
|
33
|
+
var getDifference = require('./getDifference.cjs');
|
|
32
34
|
var makeSafeTimestamp = require('./makeSafeTimestamp.cjs');
|
|
33
35
|
var createTime = require('./createTime.cjs');
|
|
34
36
|
var createTimeOrThrow = require('./createTimeOrThrow.cjs');
|
|
@@ -113,6 +115,7 @@ exports.tomorrow = tomorrow.tomorrow;
|
|
|
113
115
|
exports.toNative = toNative.toNative;
|
|
114
116
|
exports.toTimestamp = toTimestamp.toTimestamp;
|
|
115
117
|
exports.toTimeValue = toTimeValue.toTimeValue;
|
|
118
|
+
exports.computeTime = computeTime.computeTime;
|
|
116
119
|
exports.toISOString = toISOString.toISOString;
|
|
117
120
|
exports.closestTo = closestTo.closestTo;
|
|
118
121
|
exports.each = each.each;
|
|
@@ -134,6 +137,7 @@ exports.getTimezoneOffset = getTimezoneOffset.getTimezoneOffset;
|
|
|
134
137
|
exports.applyTimezone = applyTimezone.applyTimezone;
|
|
135
138
|
exports.format = format.format;
|
|
136
139
|
exports.formatTime = formatTime.formatTime;
|
|
140
|
+
exports.getDifference = getDifference.getDifference;
|
|
137
141
|
exports.makeSafeTimestamp = makeSafeTimestamp.makeSafeTimestamp;
|
|
138
142
|
exports.createTime = createTime.createTime;
|
|
139
143
|
exports.CreateTheTimeError = createTimeOrThrow.CreateTheTimeError;
|
package/dist/date/index.d.ts
CHANGED
|
@@ -40,6 +40,7 @@ export * from "./tomorrow";
|
|
|
40
40
|
export * from "./toNative";
|
|
41
41
|
export * from "./toTimestamp";
|
|
42
42
|
export * from "./toTimeValue";
|
|
43
|
+
export * from "./computeTime";
|
|
43
44
|
export * from "./getters";
|
|
44
45
|
export * from "./toISOString";
|
|
45
46
|
export * from "./operators";
|
|
@@ -63,6 +64,7 @@ export * from "./getTimezoneOffset";
|
|
|
63
64
|
export * from "./applyTimezone";
|
|
64
65
|
export * from "./format";
|
|
65
66
|
export * from "./formatTime";
|
|
67
|
+
export * from "./getDifference";
|
|
66
68
|
export * from "./makeSafeTimestamp";
|
|
67
69
|
export * from "./createTime";
|
|
68
70
|
export * from "./createTimeOrThrow";
|
package/dist/date/index.mjs
CHANGED
|
@@ -7,6 +7,7 @@ export { tomorrow } from './tomorrow.mjs';
|
|
|
7
7
|
export { toNative } from './toNative.mjs';
|
|
8
8
|
export { toTimestamp } from './toTimestamp.mjs';
|
|
9
9
|
export { toTimeValue } from './toTimeValue.mjs';
|
|
10
|
+
export { computeTime } from './computeTime.mjs';
|
|
10
11
|
export { toISOString } from './toISOString.mjs';
|
|
11
12
|
export { closestTo } from './closestTo.mjs';
|
|
12
13
|
export { each } from './each.mjs';
|
|
@@ -27,6 +28,7 @@ export { getTimezoneOffset } from './getTimezoneOffset.mjs';
|
|
|
27
28
|
export { applyTimezone } from './applyTimezone.mjs';
|
|
28
29
|
export { format } from './format.mjs';
|
|
29
30
|
export { formatTime } from './formatTime.mjs';
|
|
31
|
+
export { getDifference } from './getDifference.mjs';
|
|
30
32
|
export { makeSafeTimestamp } from './makeSafeTimestamp.mjs';
|
|
31
33
|
export { createTime } from './createTime.mjs';
|
|
32
34
|
export { CreateTheTimeError, createTimeOrThrow } from './createTimeOrThrow.mjs';
|
package/dist/date/theTime.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { SerializedTheTime } from "./types";
|
|
2
2
|
declare const TheTime_base: new (params?: {
|
|
3
3
|
"@DuplojsUtilsDate/the-time"?: unknown;
|
|
4
4
|
} | undefined) => import("../common/kind").Kind<import("../common/kind").KindDefinition<"the-time", unknown>, unknown> & import("../common/kind").Kind<import("../common/kind").KindDefinition<"@DuplojsUtilsDate/the-time", unknown>, unknown>;
|
|
5
5
|
/**
|
|
6
|
-
* Represents an immutable time value object (`TheTime`) stored in milliseconds.
|
|
6
|
+
* Represents an immutable signed time value object (`TheTime`) stored in milliseconds.
|
|
7
7
|
*
|
|
8
8
|
* Signature: `TheTime` → immutable class-like value object
|
|
9
9
|
*
|
|
@@ -32,6 +32,7 @@ declare const TheTime_base: new (params?: {
|
|
|
32
32
|
*
|
|
33
33
|
* @remarks
|
|
34
34
|
* - `TheTime` is not a `number`, but it behaves as an immutable numeric time value.
|
|
35
|
+
* - `TheTime` supports negative values and is not an absolute value by default.
|
|
35
36
|
* - Date unit operators (`addMinutes`, `subtractDays`, etc.) target `TheDate`; `TheTime` is used as a normalized duration value.
|
|
36
37
|
* - Use `D.serialize(theTime)` to convert to `SerializedTheTime`.
|
|
37
38
|
* - Use `D.createTime(...)` / `D.createTimeOrThrow(...)` to construct instances.
|
package/dist/date/toNative.cjs
CHANGED
|
@@ -1,17 +1,23 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var theDate = require('./theDate.cjs');
|
|
4
|
+
var theTime = require('./theTime.cjs');
|
|
4
5
|
var toTimestamp = require('./toTimestamp.cjs');
|
|
6
|
+
var toTimeValue = require('./toTimeValue.cjs');
|
|
5
7
|
|
|
6
|
-
/**
|
|
7
|
-
* {@include date/toNative/index.md}
|
|
8
|
-
*/
|
|
9
8
|
function toNative(input) {
|
|
10
9
|
if (input instanceof theDate.TheDate) {
|
|
11
10
|
return input.toNative();
|
|
12
11
|
}
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
else if (input instanceof theTime.TheTime) {
|
|
13
|
+
return input.toNative();
|
|
14
|
+
}
|
|
15
|
+
else if (input.startsWith("date")) {
|
|
16
|
+
return new Date(toTimestamp.toTimestamp(input));
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
return toTimeValue.toTimeValue(input);
|
|
20
|
+
}
|
|
15
21
|
}
|
|
16
22
|
|
|
17
23
|
exports.toNative = toNative;
|
package/dist/date/toNative.d.ts
CHANGED
|
@@ -1,28 +1,42 @@
|
|
|
1
1
|
import { TheDate } from "./theDate";
|
|
2
|
-
import
|
|
2
|
+
import { TheTime } from "./theTime";
|
|
3
|
+
import type { SerializedTheDate, SerializedTheTime } from "./types";
|
|
3
4
|
/**
|
|
4
|
-
* Converts a date value to native JavaScript
|
|
5
|
+
* Converts a date or time value to native JavaScript representation.
|
|
5
6
|
*
|
|
6
|
-
* Signature: `toNative(input)` → `Date`
|
|
7
|
+
* Signature: `toNative(input)` → `Date | number`
|
|
7
8
|
*
|
|
8
|
-
* `input` accepts `TheDate` or `
|
|
9
|
+
* `input` accepts `TheDate`, `SerializedTheDate`, `TheTime`, or `SerializedTheTime`.
|
|
9
10
|
*
|
|
10
11
|
* ```ts
|
|
11
|
-
* const
|
|
12
|
-
* const nativeDate = D.toNative(
|
|
12
|
+
* const dateInput = D.create("2024-06-20");
|
|
13
|
+
* const nativeDate = D.toNative(dateInput);
|
|
13
14
|
* // nativeDate: Date
|
|
14
15
|
*
|
|
15
16
|
* const nativeDate2 = D.toNative("date1718841600000+");
|
|
16
17
|
* // nativeDate2: Date
|
|
17
18
|
*
|
|
19
|
+
* const timeInput = D.createTime(90000, "millisecond");
|
|
20
|
+
* const timeValue = D.toNative(timeInput);
|
|
21
|
+
* // timeValue: number
|
|
22
|
+
*
|
|
23
|
+
* const timeValue2 = D.toNative("time3600000-");
|
|
24
|
+
* // timeValue2: number
|
|
25
|
+
*
|
|
18
26
|
* pipe(
|
|
19
|
-
*
|
|
27
|
+
* timeInput,
|
|
20
28
|
* D.toNative,
|
|
29
|
+
* ); // number
|
|
21
30
|
* ```
|
|
22
31
|
*
|
|
32
|
+
* @remarks
|
|
33
|
+
* - Returns `Date` for date inputs.
|
|
34
|
+
* - Returns `number` for time inputs.
|
|
35
|
+
*
|
|
23
36
|
* @see https://utils.duplojs.dev/en/v1/api/date/toNative
|
|
24
37
|
*
|
|
25
38
|
* @namespace D
|
|
26
39
|
*
|
|
27
40
|
*/
|
|
28
41
|
export declare function toNative<GenericInput extends TheDate | SerializedTheDate>(input: GenericInput): Date;
|
|
42
|
+
export declare function toNative<GenericInput extends TheTime | SerializedTheTime>(input: GenericInput): number;
|
package/dist/date/toNative.mjs
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
import { TheDate } from './theDate.mjs';
|
|
2
|
+
import { TheTime } from './theTime.mjs';
|
|
2
3
|
import { toTimestamp } from './toTimestamp.mjs';
|
|
4
|
+
import { toTimeValue } from './toTimeValue.mjs';
|
|
3
5
|
|
|
4
|
-
/**
|
|
5
|
-
* {@include date/toNative/index.md}
|
|
6
|
-
*/
|
|
7
6
|
function toNative(input) {
|
|
8
7
|
if (input instanceof TheDate) {
|
|
9
8
|
return input.toNative();
|
|
10
9
|
}
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
else if (input instanceof TheTime) {
|
|
11
|
+
return input.toNative();
|
|
12
|
+
}
|
|
13
|
+
else if (input.startsWith("date")) {
|
|
14
|
+
return new Date(toTimestamp(input));
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
return toTimeValue(input);
|
|
18
|
+
}
|
|
13
19
|
}
|
|
14
20
|
|
|
15
21
|
export { toNative };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Represents an immutable time value object (`TheTime`) stored in milliseconds.
|
|
2
|
+
* Represents an immutable signed time value object (`TheTime`) stored in milliseconds.
|
|
3
3
|
*
|
|
4
4
|
* Signature: `TheTime` → immutable class-like value object
|
|
5
5
|
*
|
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
*
|
|
29
29
|
* @remarks
|
|
30
30
|
* - `TheTime` is not a `number`, but it behaves as an immutable numeric time value.
|
|
31
|
+
* - `TheTime` supports negative values and is not an absolute value by default.
|
|
31
32
|
* - Date unit operators (`addMinutes`, `subtractDays`, etc.) target `TheDate`; `TheTime` is used as a normalized duration value.
|
|
32
33
|
* - Use `D.serialize(theTime)` to convert to `SerializedTheTime`.
|
|
33
34
|
* - Use `D.createTime(...)` / `D.createTimeOrThrow(...)` to construct instances.
|