@develia/commons 0.3.9 → 0.3.12

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.
@@ -0,0 +1,18 @@
1
+ export default class Timer {
2
+ private _callback;
3
+ private _interval;
4
+ private _intervalId;
5
+ /**
6
+ * @param callback Callback
7
+ * @param interval Seconds between calls
8
+ */
9
+ constructor(callback: () => void, interval: number);
10
+ get running(): boolean;
11
+ get interval(): number;
12
+ set interval(value: number);
13
+ get callback(): () => void;
14
+ set callback(value: () => void);
15
+ start(): void;
16
+ stop(): void;
17
+ restart(): void;
18
+ }
@@ -0,0 +1,46 @@
1
+ export default class TimeSpan {
2
+ milliseconds: number;
3
+ private constructor();
4
+ seconds(): number;
5
+ minutes(): number;
6
+ hours(): number;
7
+ days(): number;
8
+ weeks(): number;
9
+ static fromMilliseconds(milliseconds: number): TimeSpan;
10
+ static fromSeconds(seconds: number): TimeSpan;
11
+ static fromMinutes(minutes: number): TimeSpan;
12
+ static fromHours(hours: number): TimeSpan;
13
+ static fromDays(days: number): TimeSpan;
14
+ static fromWeeks(weeks: number): TimeSpan;
15
+ addMilliseconds(milliseconds: number): TimeSpan;
16
+ addSeconds(seconds: number): TimeSpan;
17
+ addMinutes(minutes: number): TimeSpan;
18
+ addHours(hours: number): TimeSpan;
19
+ addDays(days: number): TimeSpan;
20
+ addWeeks(weeks: number): TimeSpan;
21
+ subtractMilliseconds(milliseconds: number): TimeSpan;
22
+ subtractSeconds(seconds: number): TimeSpan;
23
+ subtractMinutes(minutes: number): TimeSpan;
24
+ subtractHours(hours: number): TimeSpan;
25
+ subtractDays(days: number): TimeSpan;
26
+ subtractWeeks(weeks: number): TimeSpan;
27
+ add(other: TimeSpan): TimeSpan;
28
+ subtract(other: TimeSpan): TimeSpan;
29
+ addTo(date: Date): Date;
30
+ subtractFrom(date: Date): Date;
31
+ static fromDifference(earlierDate: Date, laterDate: Date): TimeSpan;
32
+ format(format?: string): string;
33
+ eq(other: TimeSpan): boolean;
34
+ le(other: TimeSpan): boolean;
35
+ lt(other: TimeSpan): boolean;
36
+ ge(other: TimeSpan): boolean;
37
+ gt(other: TimeSpan): boolean;
38
+ multiply(number: number): TimeSpan;
39
+ divide(number: number): TimeSpan;
40
+ abs(): TimeSpan;
41
+ static readonly INFINITE: TimeSpan;
42
+ static readonly NEGATIVE_INFINITE: TimeSpan;
43
+ isInfinite(): boolean;
44
+ isPositiveInfinite(): boolean;
45
+ isNegativeInfinite(): boolean;
46
+ }
@@ -1,14 +1,7 @@
1
- export type EqualityComparer<T> = (arg1: T, arg2: T) => boolean;
2
-
3
- export type UnaryFunction<T, R> = (arg: T) => R;
4
-
5
- export type Consumer<T> = (arg: T) => void
6
-
7
- export type Predicate<T> = (arg: T) => boolean;
8
-
9
- export type Provider<T> = () => T;
10
-
11
- export type Nullable<T> = T | null;
12
-
13
- export type Deferred<T> = Promise<T> | Provider<T>
14
-
1
+ export type EqualityComparer<T> = (arg1: T, arg2: T) => boolean;
2
+ export type UnaryFunction<T, R> = (arg: T) => R;
3
+ export type Consumer<T> = (arg: T) => void;
4
+ export type Predicate<T> = (arg: T) => boolean;
5
+ export type Provider<T> = () => T;
6
+ export type Nullable<T> = T | null;
7
+ export type Deferred<T> = Promise<T> | Provider<T>;
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@develia/commons",
3
- "version": "0.3.9",
3
+ "version": "0.3.12",
4
4
  "author": {
5
5
  "name": "Antonio Gil Espinosa",
6
6
  "email": "antonio.gil.espinosa@gmail.com",
7
7
  "url": "https://antoniogilespinosa.com"
8
8
  },
9
- "types": "dist/index.d.ts",
9
+ "types": "dist/types/index.d.ts",
10
10
  "main": "dist/index.cjs.js",
11
11
  "module": "dist/index.esm.js",
12
12
  "license": "proprietary",
@@ -15,11 +15,16 @@
15
15
  "@rollup/plugin-node-resolve": "^15.2.3",
16
16
  "@types/node": "^20.12.12",
17
17
  "rollup": "^2.79.1",
18
- "rollup-plugin-terser": "^7.0.2",
19
- "typescript": "^5.1.3",
20
- "rollup-plugin-typescript2": "^0.36.0"
18
+ "typescript": "^5.1.3"
21
19
  },
22
20
  "scripts": {
23
- "build": "rollup -c"
24
- }
21
+ "build": "rollup -c",
22
+ "watch": "rollup -c -w"
23
+ },
24
+ "dependencies": {
25
+ "@rollup/plugin-typescript": "^11.1.6"
26
+ },
27
+ "files": [
28
+ "dist"
29
+ ]
25
30
  }
package/rollup.config.js DELETED
@@ -1,27 +0,0 @@
1
- // rollup.config.js
2
- import resolve from '@rollup/plugin-node-resolve';
3
- import commonjs from '@rollup/plugin-commonjs';
4
- import typescript from 'rollup-plugin-typescript2';
5
-
6
- export default {
7
- input: 'src/index.ts',
8
- output: [{
9
- file: 'dist/index.cjs.js',
10
- format: 'cjs'
11
- }, {
12
- file: 'dist/index.esm.js',
13
- format: 'esm'
14
- }/*, {
15
- file: 'dist/index.umd.js',
16
- format: 'umd',
17
- sourcemap: true,
18
- name: 'develia',
19
- }*/],
20
- plugins: [
21
- resolve(),
22
- commonjs(),
23
- typescript({
24
- tsconfig: "tsconfig.json"
25
- })
26
- ]
27
- };
@@ -1,78 +0,0 @@
1
- export default class CacheDictionary<T> {
2
- private readonly fallback: ((key: string) => T) | null;
3
- private readonly cache: { [key: string]: T };
4
- private readonly defaultDuration: number | null;
5
- private readonly expiration: { [key: string]: Date | undefined };
6
-
7
- constructor(fallback: ((key: string) => T) | null = null, defaultDuration: number | null = null) {
8
- this.fallback = fallback;
9
- this.cache = {};
10
- this.defaultDuration = defaultDuration;
11
- this.expiration = {};
12
- }
13
-
14
-
15
- public getExpiration(key: string): Date | undefined {
16
- return this.expiration[key];
17
- }
18
-
19
- public setExpiration(key: string, expiration: Date | undefined): void {
20
- this.expiration[key] = expiration;
21
- }
22
-
23
- public setDuration(key: string, duration: number | null): void {
24
- if (duration === null) {
25
- delete this.expiration[key];
26
- } else {
27
- const expiration = new Date();
28
- expiration.setSeconds(expiration.getSeconds() + duration);
29
- this.expiration[key] = expiration;
30
- }
31
- }
32
-
33
- public get(key: string): T | null {
34
- if (!this.cache.hasOwnProperty(key)) {
35
- if (this.fallback) {
36
- this.cache[key] = this.fallback(key);
37
- this.expiration[key] = this.calculateExpiration(this.defaultDuration);
38
- } else {
39
- return null;
40
- }
41
- }
42
-
43
- if (this.isExpired(key)) {
44
- this.remove(key);
45
- return null;
46
- }
47
-
48
- return this.cache[key];
49
- }
50
-
51
- public set(key: string, value: T, duration: number | null = null): void {
52
- this.cache[key] = value;
53
- this.expiration[key] = this.calculateExpiration(duration);
54
- }
55
-
56
- public remove(key: string): void {
57
- delete this.cache[key];
58
- delete this.expiration[key];
59
- }
60
-
61
- private isExpired(key: string): boolean {
62
- const expiration = this.expiration[key];
63
- if (expiration instanceof Date) {
64
- return expiration < new Date();
65
- }
66
- return false;
67
- }
68
-
69
- private calculateExpiration(duration: number | null): Date | undefined {
70
- if (duration === null) {
71
- return undefined;
72
- }
73
-
74
- const expiration = new Date();
75
- expiration.setSeconds(expiration.getSeconds() + duration);
76
- return expiration;
77
- }
78
- }