@sparted/shared-library 1.0.0 → 1.0.1
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/array/get-values-from-keys.js +1 -3
- package/array/get-values-from-keys.js.map +1 -1
- package/cdn/cdn-creators/imgix.cdn-creator.d.ts +3 -0
- package/cdn/cdn-creators/imgix.cdn-creator.js +24 -0
- package/cdn/cdn-creators/imgix.cdn-creator.js.map +1 -0
- package/cdn/cdn-creators/index.d.ts +2 -0
- package/cdn/cdn-creators/index.js +14 -0
- package/cdn/cdn-creators/index.js.map +1 -0
- package/cdn/cdn-creators/twicpics.cdn-creator.d.ts +3 -0
- package/cdn/cdn-creators/twicpics.cdn-creator.js +30 -0
- package/cdn/cdn-creators/twicpics.cdn-creator.js.map +1 -0
- package/cdn/cdn.factory.d.ts +4 -0
- package/cdn/cdn.factory.js +19 -0
- package/cdn/cdn.factory.js.map +1 -0
- package/cdn/index.d.ts +2 -0
- package/cdn/index.js +10 -0
- package/cdn/index.js.map +1 -0
- package/cdn/types/cdn-factory.type.d.ts +24 -0
- package/cdn/types/index.d.ts +2 -0
- package/cdn/types/index.js +3 -0
- package/cdn/types/index.js.map +1 -0
- package/cdn/types/transformation.type.d.ts +17 -0
- package/cdn/utils/extract-info-from-url.util.d.ts +6 -0
- package/cdn/utils/extract-info-from-url.util.js +17 -0
- package/cdn/utils/extract-info-from-url.util.js.map +1 -0
- package/cdn/utils/get-cdn-from-s3-url.util.d.ts +7 -0
- package/cdn/utils/get-cdn-from-s3-url.util.js +22 -0
- package/cdn/utils/get-cdn-from-s3-url.util.js.map +1 -0
- package/cdn/utils/index.d.ts +2 -0
- package/cdn/utils/index.js +12 -0
- package/cdn/utils/index.js.map +1 -0
- package/index.d.ts +1 -0
- package/index.js +2 -0
- package/index.js.map +1 -1
- package/linked-list/apply-diff-linked-list.js.map +1 -1
- package/linked-list/create-linked-list.js +2 -2
- package/linked-list/create-linked-list.js.map +1 -1
- package/linked-list/find-linked-list.js +15 -17
- package/linked-list/find-linked-list.js.map +1 -1
- package/linked-list/get-linked-list.js.map +1 -1
- package/linked-list/linked-list-utils.js +3 -3
- package/linked-list/linked-list-utils.js.map +1 -1
- package/linked-list/reduce-linked-list.js +3 -5
- package/linked-list/reduce-linked-list.js.map +1 -1
- package/linked-list/remove-linked-list.js +1 -1
- package/linked-list/remove-linked-list.js.map +1 -1
- package/linked-list/update-linked-list.js.map +1 -1
- package/linked-list/while-linked-list.js.map +1 -1
- package/math/clamp-number.js.map +1 -1
- package/math/interpolate.js.map +1 -1
- package/package.json +12 -1
- package/parsing/parse-boolean.js.map +1 -1
- package/parsing/parse-enum.js.map +1 -1
- package/parsing/parse-number.js.map +1 -1
- package/predicates/is-function.js +1 -3
- package/predicates/is-function.js.map +1 -1
- package/predicates/is-null.js +1 -3
- package/predicates/is-null.js.map +1 -1
- package/predicates/is-object.js +1 -3
- package/predicates/is-object.js.map +1 -1
- package/predicates/is-primitive.js +1 -3
- package/predicates/is-primitive.js.map +1 -1
- package/predicates/is-undefined.js +1 -3
- package/predicates/is-undefined.js.map +1 -1
- package/time/constants.js.map +1 -1
- package/time/convert.js.map +1 -1
- package/time/get-time-diff-as-string.js.map +1 -1
|
@@ -2,9 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
const valuesFromKeys = (keys, map) =>
|
|
6
|
-
return keys.map((key) => map[key]);
|
|
7
|
-
};
|
|
5
|
+
const valuesFromKeys = (keys, map) => (keys.map((key) => map[key]));
|
|
8
6
|
|
|
9
7
|
exports.valuesFromKeys = valuesFromKeys;
|
|
10
8
|
//# sourceMappingURL=get-values-from-keys.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"get-values-from-keys.js","sources":["../../src/array/get-values-from-keys.ts"],"sourcesContent":["export const valuesFromKeys = <T>(keys: string[], map: Record<string, T>) =>
|
|
1
|
+
{"version":3,"file":"get-values-from-keys.js","sources":["../../src/array/get-values-from-keys.ts"],"sourcesContent":["export const valuesFromKeys = <T>(keys: string[], map: Record<string, T>) => (\n keys.map((key) => map[key])\n);\n"],"names":[],"mappings":";;;;AAAa,MAAA,cAAc,GAAG,CAAI,IAAc,EAAE,GAAsB,MACtE,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,KAAK,GAAG,CAAC,GAAG,CAAC,CAAC;;;;"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var getCdnFromS3Url_util = require('../utils/get-cdn-from-s3-url.util.js');
|
|
6
|
+
|
|
7
|
+
const imgixTransformationQueries = (params) => [
|
|
8
|
+
'?auto=compress,format',
|
|
9
|
+
...Object.entries(params).map(([k, v]) => `&${k}=${v}`),
|
|
10
|
+
].join('');
|
|
11
|
+
const imgixCdnCreator = (domain) => {
|
|
12
|
+
const getCdnUrl = (path) => `${domain}${path}`;
|
|
13
|
+
return {
|
|
14
|
+
getCdnUrl,
|
|
15
|
+
getCdnFromS3Url: getCdnFromS3Url_util.getCdnFromS3UrlFactory({
|
|
16
|
+
getCdnUrl,
|
|
17
|
+
transformationQueries: imgixTransformationQueries,
|
|
18
|
+
}),
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
exports.imgixCdnCreator = imgixCdnCreator;
|
|
23
|
+
exports.imgixTransformationQueries = imgixTransformationQueries;
|
|
24
|
+
//# sourceMappingURL=imgix.cdn-creator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"imgix.cdn-creator.js","sources":["../../../src/cdn/cdn-creators/imgix.cdn-creator.ts"],"sourcesContent":["import type {\n CreateCdn,\n GetCdnUrl,\n ImgixConfig,\n ImgixTransformation,\n TransformationQueries,\n} from '../types';\nimport { getCdnFromS3UrlFactory } from '../utils/get-cdn-from-s3-url.util';\n\nexport const imgixTransformationQueries: TransformationQueries<\nImgixTransformation\n> = (params) => [\n '?auto=compress,format',\n ...Object.entries(params).map(([k, v]) => `&${k}=${v}`),\n].join('');\n\nexport const imgixCdnCreator: CreateCdn<ImgixConfig> = (domain) => {\n const getCdnUrl: GetCdnUrl = (path) => `${domain}${path}`;\n\n return {\n getCdnUrl,\n getCdnFromS3Url: getCdnFromS3UrlFactory({\n getCdnUrl,\n transformationQueries: imgixTransformationQueries,\n }),\n };\n};\n"],"names":["getCdnFromS3UrlFactory"],"mappings":";;;;;;MASa,0BAA0B,GAEnC,CAAC,MAAM,KAAK;IACd,uBAAuB;IACvB,GAAG,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,IAAI,CAAC,CAAA,CAAA,EAAI,CAAC,CAAA,CAAE,CAAC;AACxD,CAAA,CAAC,IAAI,CAAC,EAAE,EAAE;AAEE,MAAA,eAAe,GAA2B,CAAC,MAAM,KAAI;AAChE,IAAA,MAAM,SAAS,GAAc,CAAC,IAAI,KAAK,CAAA,EAAG,MAAM,CAAA,EAAG,IAAI,CAAA,CAAE,CAAC;IAE1D,OAAO;QACL,SAAS;QACT,eAAe,EAAEA,2CAAsB,CAAC;YACtC,SAAS;AACT,YAAA,qBAAqB,EAAE,0BAA0B;SAClD,CAAC;KACH,CAAC;AACJ;;;;;"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var imgix_cdnCreator = require('./imgix.cdn-creator.js');
|
|
6
|
+
var twicpics_cdnCreator = require('./twicpics.cdn-creator.js');
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
exports.imgixCdnCreator = imgix_cdnCreator.imgixCdnCreator;
|
|
11
|
+
exports.imgixTransformationQueries = imgix_cdnCreator.imgixTransformationQueries;
|
|
12
|
+
exports.twicpicsCdnCreator = twicpics_cdnCreator.twicpicsCdnCreator;
|
|
13
|
+
exports.twicpicsTransformationQueries = twicpics_cdnCreator.twicpicsTransformationQueries;
|
|
14
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { CreateCdn, TransformationQueries, TwicpicsConfig, TwicpicsTransformation } from '../types';
|
|
2
|
+
export declare const twicpicsTransformationQueries: TransformationQueries<TwicpicsTransformation>;
|
|
3
|
+
export declare const twicpicsCdnCreator: CreateCdn<TwicpicsConfig>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var getCdnFromS3Url_util = require('../utils/get-cdn-from-s3-url.util.js');
|
|
6
|
+
|
|
7
|
+
const twicpicsTransformationQueries = (params) => {
|
|
8
|
+
if (params.fit !== undefined) {
|
|
9
|
+
return `?twic=v1/${params.fit}=${params.width}x${params.height}`;
|
|
10
|
+
}
|
|
11
|
+
if (params.width !== undefined) {
|
|
12
|
+
return `?twic=v1/resize=${params.width}`;
|
|
13
|
+
}
|
|
14
|
+
return '';
|
|
15
|
+
};
|
|
16
|
+
const twicpicsCdnCreator = (domain, config) => {
|
|
17
|
+
const { baseUrlMapper } = config;
|
|
18
|
+
const getCdnUrl = (path, instanceId) => `${domain}${baseUrlMapper(instanceId)}${path}`;
|
|
19
|
+
return {
|
|
20
|
+
getCdnUrl,
|
|
21
|
+
getCdnFromS3Url: getCdnFromS3Url_util.getCdnFromS3UrlFactory({
|
|
22
|
+
getCdnUrl,
|
|
23
|
+
transformationQueries: twicpicsTransformationQueries,
|
|
24
|
+
}),
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
exports.twicpicsCdnCreator = twicpicsCdnCreator;
|
|
29
|
+
exports.twicpicsTransformationQueries = twicpicsTransformationQueries;
|
|
30
|
+
//# sourceMappingURL=twicpics.cdn-creator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"twicpics.cdn-creator.js","sources":["../../../src/cdn/cdn-creators/twicpics.cdn-creator.ts"],"sourcesContent":["import type {\n CreateCdn,\n GetCdnUrl,\n TransformationQueries,\n TwicpicsConfig,\n TwicpicsTransformation,\n} from '../types';\nimport { getCdnFromS3UrlFactory } from '../utils';\n\nexport const twicpicsTransformationQueries: TransformationQueries<\nTwicpicsTransformation\n> = (params) => {\n if (params.fit !== undefined) {\n return `?twic=v1/${params.fit}=${params.width}x${params.height}`;\n }\n if (params.width !== undefined) {\n return `?twic=v1/resize=${params.width}`;\n }\n return '';\n};\n\nexport const twicpicsCdnCreator: CreateCdn<TwicpicsConfig> = (\n domain,\n config,\n) => {\n const { baseUrlMapper } = config;\n const getCdnUrl: GetCdnUrl = (path, instanceId) => `${domain}${baseUrlMapper(instanceId)}${path}`;\n\n return {\n getCdnUrl,\n getCdnFromS3Url: getCdnFromS3UrlFactory({\n getCdnUrl,\n transformationQueries: twicpicsTransformationQueries,\n }),\n };\n};\n"],"names":["getCdnFromS3UrlFactory"],"mappings":";;;;;;AASa,MAAA,6BAA6B,GAEtC,CAAC,MAAM,KAAI;AACb,IAAA,IAAI,MAAM,CAAC,GAAG,KAAK,SAAS,EAAE;AAC5B,QAAA,OAAO,CAAY,SAAA,EAAA,MAAM,CAAC,GAAG,CAAI,CAAA,EAAA,MAAM,CAAC,KAAK,CAAI,CAAA,EAAA,MAAM,CAAC,MAAM,EAAE,CAAC;AAClE,KAAA;AACD,IAAA,IAAI,MAAM,CAAC,KAAK,KAAK,SAAS,EAAE;AAC9B,QAAA,OAAO,CAAmB,gBAAA,EAAA,MAAM,CAAC,KAAK,EAAE,CAAC;AAC1C,KAAA;AACD,IAAA,OAAO,EAAE,CAAC;AACZ,EAAE;MAEW,kBAAkB,GAA8B,CAC3D,MAAM,EACN,MAAM,KACJ;AACF,IAAA,MAAM,EAAE,aAAa,EAAE,GAAG,MAAM,CAAC;AACjC,IAAA,MAAM,SAAS,GAAc,CAAC,IAAI,EAAE,UAAU,KAAK,GAAG,MAAM,CAAA,EAAG,aAAa,CAAC,UAAU,CAAC,CAAG,EAAA,IAAI,EAAE,CAAC;IAElG,OAAO;QACL,SAAS;QACT,eAAe,EAAEA,2CAAsB,CAAC;YACtC,SAAS;AACT,YAAA,qBAAqB,EAAE,6BAA6B;SACrD,CAAC;KACH,CAAC;AACJ;;;;;"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { Cdn, ImgixConfig, TwicpicsConfig } from './types';
|
|
2
|
+
declare type WhichCdn<Config> = Config extends ImgixConfig ? Cdn<ImgixConfig> : Cdn<TwicpicsConfig>;
|
|
3
|
+
export declare function cdnFactory<Config extends ImgixConfig | TwicpicsConfig>(domain: string, config: Config): WhichCdn<Config>;
|
|
4
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var imgix_cdnCreator = require('./cdn-creators/imgix.cdn-creator.js');
|
|
6
|
+
var twicpics_cdnCreator = require('./cdn-creators/twicpics.cdn-creator.js');
|
|
7
|
+
|
|
8
|
+
function cdnFactory(domain, config) {
|
|
9
|
+
if (config.type === 'twicpics') {
|
|
10
|
+
return twicpics_cdnCreator.twicpicsCdnCreator(domain, config);
|
|
11
|
+
}
|
|
12
|
+
if (config.type === 'imgix') {
|
|
13
|
+
return imgix_cdnCreator.imgixCdnCreator(domain);
|
|
14
|
+
}
|
|
15
|
+
throw new Error('Unknown cdn type');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
exports.cdnFactory = cdnFactory;
|
|
19
|
+
//# sourceMappingURL=cdn.factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cdn.factory.js","sources":["../../src/cdn/cdn.factory.ts"],"sourcesContent":["import { imgixCdnCreator, twicpicsCdnCreator } from './cdn-creators';\nimport type { Cdn, ImgixConfig, TwicpicsConfig } from './types';\n\ntype WhichCdn<Config> = Config extends ImgixConfig\n ? Cdn<ImgixConfig>\n : Cdn<TwicpicsConfig>;\n\nexport function cdnFactory<Config extends ImgixConfig | TwicpicsConfig>(\n domain: string,\n config: Config,\n): WhichCdn<Config> {\n if (config.type === 'twicpics') {\n return twicpicsCdnCreator(domain, config) as WhichCdn<Config>;\n }\n if (config.type === 'imgix') {\n return imgixCdnCreator(domain, config) as WhichCdn<Config>;\n }\n throw new Error('Unknown cdn type');\n}\n"],"names":["twicpicsCdnCreator","imgixCdnCreator"],"mappings":";;;;;;;AAOgB,SAAA,UAAU,CACxB,MAAc,EACd,MAAc,EAAA;AAEd,IAAA,IAAI,MAAM,CAAC,IAAI,KAAK,UAAU,EAAE;AAC9B,QAAA,OAAOA,sCAAkB,CAAC,MAAM,EAAE,MAAM,CAAqB,CAAC;AAC/D,KAAA;AACD,IAAA,IAAI,MAAM,CAAC,IAAI,KAAK,OAAO,EAAE;AAC3B,QAAA,OAAOC,gCAAe,CAAC,MAAc,CAAqB,CAAC;AAC5D,KAAA;AACD,IAAA,MAAM,IAAI,KAAK,CAAC,kBAAkB,CAAC,CAAC;AACtC;;;;"}
|
package/cdn/index.d.ts
ADDED
package/cdn/index.js
ADDED
package/cdn/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ImgixTransformation, Transformation, TwicpicsTransformation } from './transformation.type';
|
|
2
|
+
export declare type BaseConfig = {
|
|
3
|
+
type: 'twicpics' | 'imgix';
|
|
4
|
+
};
|
|
5
|
+
export declare type BaseUrlMapperFunc = (instanceId: string) => string;
|
|
6
|
+
export declare type TwicpicsConfig = BaseConfig & {
|
|
7
|
+
type: 'twicpics';
|
|
8
|
+
baseUrlMapper: BaseUrlMapperFunc;
|
|
9
|
+
};
|
|
10
|
+
export declare type ImgixConfig = BaseConfig & {
|
|
11
|
+
type: 'imgix';
|
|
12
|
+
};
|
|
13
|
+
export declare type CdnParams<T extends Transformation> = {
|
|
14
|
+
asThumbnail?: boolean;
|
|
15
|
+
transformation?: T;
|
|
16
|
+
};
|
|
17
|
+
export declare type GetCdnUrl = (path: string, instanceId: string) => string;
|
|
18
|
+
export declare type GetCdnFromS3Url<T extends Transformation> = (url: string, params?: CdnParams<T>) => string;
|
|
19
|
+
export declare type TransformationFromConfig<Config extends BaseConfig> = Config['type'] extends 'imgix' ? ImgixTransformation : TwicpicsTransformation;
|
|
20
|
+
export declare type Cdn<Config extends BaseConfig> = {
|
|
21
|
+
getCdnUrl: GetCdnUrl;
|
|
22
|
+
getCdnFromS3Url: GetCdnFromS3Url<TransformationFromConfig<Config>>;
|
|
23
|
+
};
|
|
24
|
+
export declare type CreateCdn<Config extends BaseConfig> = (domain: string, config: Config) => Cdn<typeof config>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export declare type Transformation = {
|
|
2
|
+
width?: number;
|
|
3
|
+
height?: number;
|
|
4
|
+
};
|
|
5
|
+
export declare type TwicpicsTransformation = Transformation & ({
|
|
6
|
+
fit: 'crop' | 'contain';
|
|
7
|
+
width: number;
|
|
8
|
+
height: number;
|
|
9
|
+
} | {
|
|
10
|
+
fit?: undefined;
|
|
11
|
+
width: number;
|
|
12
|
+
height?: undefined;
|
|
13
|
+
});
|
|
14
|
+
export declare type ImgixTransformation = Transformation & {
|
|
15
|
+
fit?: 'crop';
|
|
16
|
+
};
|
|
17
|
+
export declare type TransformationQueries<T extends Transformation> = (params: T) => string;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
const S3_URL_RE = /https:\/\/sparted-prod-([^.]+)\.s3\.(eu-west-1)?(amazonaws\.com)?(fr-par\.scw\.cloud)?([^?]+)/;
|
|
6
|
+
const extractInfoFromUrl = (url) => {
|
|
7
|
+
const matchS3Url = url.match(S3_URL_RE);
|
|
8
|
+
if (!matchS3Url) {
|
|
9
|
+
return undefined;
|
|
10
|
+
}
|
|
11
|
+
const instanceId = matchS3Url[1];
|
|
12
|
+
const path = matchS3Url[5];
|
|
13
|
+
return { instanceId, path };
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
exports.extractInfoFromUrl = extractInfoFromUrl;
|
|
17
|
+
//# sourceMappingURL=extract-info-from-url.util.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extract-info-from-url.util.js","sources":["../../../src/cdn/utils/extract-info-from-url.util.ts"],"sourcesContent":["type ExtractInfoFromUrl = (url: string) =>\n| {\n instanceId: string;\n path: string;\n}\n| undefined;\n\nconst S3_URL_RE = /https:\\/\\/sparted-prod-([^.]+)\\.s3\\.(eu-west-1)?(amazonaws\\.com)?(fr-par\\.scw\\.cloud)?([^?]+)/;\n\nexport const extractInfoFromUrl: ExtractInfoFromUrl = (url) => {\n const matchS3Url = url.match(S3_URL_RE);\n\n if (!matchS3Url) {\n return undefined;\n }\n\n const instanceId = matchS3Url[1];\n const path = matchS3Url[5];\n\n return { instanceId, path };\n};\n"],"names":[],"mappings":";;;;AAOA,MAAM,SAAS,GAAG,+FAA+F,CAAC;AAErG,MAAA,kBAAkB,GAAuB,CAAC,GAAG,KAAI;IAC5D,MAAM,UAAU,GAAG,GAAG,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;IAExC,IAAI,CAAC,UAAU,EAAE;AACf,QAAA,OAAO,SAAS,CAAC;AAClB,KAAA;AAED,IAAA,MAAM,UAAU,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC;AACjC,IAAA,MAAM,IAAI,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC;AAE3B,IAAA,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC;AAC9B;;;;"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { GetCdnFromS3Url, GetCdnUrl, Transformation, TransformationQueries } from '../types';
|
|
2
|
+
declare type FactoryConfig<T extends Transformation> = {
|
|
3
|
+
getCdnUrl: GetCdnUrl;
|
|
4
|
+
transformationQueries: TransformationQueries<T>;
|
|
5
|
+
};
|
|
6
|
+
export declare function getCdnFromS3UrlFactory<T extends Transformation>(config: FactoryConfig<T>): GetCdnFromS3Url<T>;
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var extractInfoFromUrl_util = require('./extract-info-from-url.util.js');
|
|
6
|
+
|
|
7
|
+
const DEFAULT_THUMBNAIL_CONFIG = { width: 300, height: 300 };
|
|
8
|
+
function getCdnFromS3UrlFactory(config) {
|
|
9
|
+
return (url, params = {}) => {
|
|
10
|
+
const { getCdnUrl, transformationQueries } = config;
|
|
11
|
+
const info = extractInfoFromUrl_util.extractInfoFromUrl(url);
|
|
12
|
+
if (info === undefined) {
|
|
13
|
+
return url;
|
|
14
|
+
}
|
|
15
|
+
const { instanceId, path } = info;
|
|
16
|
+
const { transformation = params.asThumbnail ? DEFAULT_THUMBNAIL_CONFIG : {}, } = params;
|
|
17
|
+
return `${getCdnUrl(path, instanceId)}${transformationQueries(transformation)}`;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
exports.getCdnFromS3UrlFactory = getCdnFromS3UrlFactory;
|
|
22
|
+
//# sourceMappingURL=get-cdn-from-s3-url.util.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-cdn-from-s3-url.util.js","sources":["../../../src/cdn/utils/get-cdn-from-s3-url.util.ts"],"sourcesContent":["import type {\n GetCdnFromS3Url,\n GetCdnUrl,\n Transformation,\n TransformationQueries,\n} from '../types';\nimport { extractInfoFromUrl } from './extract-info-from-url.util';\n\nconst DEFAULT_THUMBNAIL_CONFIG: Transformation = { width: 300, height: 300 };\n\ntype FactoryConfig<T extends Transformation> = {\n getCdnUrl: GetCdnUrl;\n transformationQueries: TransformationQueries<T>;\n};\n\nexport function getCdnFromS3UrlFactory<T extends Transformation>(\n config: FactoryConfig<T>,\n): GetCdnFromS3Url<T> {\n return (url, params = {}) => {\n const { getCdnUrl, transformationQueries } = config;\n\n const info = extractInfoFromUrl(url);\n if (info === undefined) {\n return url;\n }\n const { instanceId, path } = info;\n\n const {\n transformation = params.asThumbnail ? DEFAULT_THUMBNAIL_CONFIG : {},\n } = params;\n\n return `${getCdnUrl(path, instanceId)}${transformationQueries(\n transformation as T,\n )}`;\n };\n}\n"],"names":["extractInfoFromUrl"],"mappings":";;;;;;AAQA,MAAM,wBAAwB,GAAmB,EAAE,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC;AAOvE,SAAU,sBAAsB,CACpC,MAAwB,EAAA;AAExB,IAAA,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,EAAE,KAAI;AAC1B,QAAA,MAAM,EAAE,SAAS,EAAE,qBAAqB,EAAE,GAAG,MAAM,CAAC;AAEpD,QAAA,MAAM,IAAI,GAAGA,0CAAkB,CAAC,GAAG,CAAC,CAAC;QACrC,IAAI,IAAI,KAAK,SAAS,EAAE;AACtB,YAAA,OAAO,GAAG,CAAC;AACZ,SAAA;AACD,QAAA,MAAM,EAAE,UAAU,EAAE,IAAI,EAAE,GAAG,IAAI,CAAC;AAElC,QAAA,MAAM,EACJ,cAAc,GAAG,MAAM,CAAC,WAAW,GAAG,wBAAwB,GAAG,EAAE,GACpE,GAAG,MAAM,CAAC;AAEX,QAAA,OAAO,CAAG,EAAA,SAAS,CAAC,IAAI,EAAE,UAAU,CAAC,CAAA,EAAG,qBAAqB,CAC3D,cAAmB,CACpB,EAAE,CAAC;AACN,KAAC,CAAC;AACJ;;;;"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var extractInfoFromUrl_util = require('./extract-info-from-url.util.js');
|
|
6
|
+
var getCdnFromS3Url_util = require('./get-cdn-from-s3-url.util.js');
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
exports.extractInfoFromUrl = extractInfoFromUrl_util.extractInfoFromUrl;
|
|
11
|
+
exports.getCdnFromS3UrlFactory = getCdnFromS3Url_util.getCdnFromS3UrlFactory;
|
|
12
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;"}
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -4,6 +4,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var arrayToMap = require('./array/array-to-map.js');
|
|
6
6
|
var getValuesFromKeys = require('./array/get-values-from-keys.js');
|
|
7
|
+
var cdn_factory = require('./cdn/cdn.factory.js');
|
|
7
8
|
var constants = require('./time/constants.js');
|
|
8
9
|
var convert = require('./time/convert.js');
|
|
9
10
|
var formatMinutes = require('./time/format-minutes.js');
|
|
@@ -35,6 +36,7 @@ var replaceInTemplate = require('./string/replace-in-template.js');
|
|
|
35
36
|
|
|
36
37
|
exports.arrayToMap = arrayToMap.arrayToMap;
|
|
37
38
|
exports.valuesFromKeys = getValuesFromKeys.valuesFromKeys;
|
|
39
|
+
exports.cdnFactory = cdn_factory.cdnFactory;
|
|
38
40
|
exports.MS_DAY = constants.MS_DAY;
|
|
39
41
|
exports.MS_HOUR = constants.MS_HOUR;
|
|
40
42
|
exports.MS_MINUTE = constants.MS_MINUTE;
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"apply-diff-linked-list.js","sources":["../../src/linked-list/apply-diff-linked-list.ts"],"sourcesContent":["import { LinkedList, LinkedListItem
|
|
1
|
+
{"version":3,"file":"apply-diff-linked-list.js","sources":["../../src/linked-list/apply-diff-linked-list.ts"],"sourcesContent":["import { LinkedList, LinkedListItem } from './type';\n\nexport const applyDiffLinkedList = <T extends LinkedListItem>(\n before: T | null,\n after: T | null,\n list: LinkedList<T>,\n) => {\n // Delete\n if (!after && before) {\n return list.remove(before.id);\n }\n\n // Insert\n if (!before && after) {\n return list.insert(after);\n }\n\n // For the following commands, none of the 2 items can be null\n // We dont use && because checks are already done above, but ts cannot infer that they cannot be null afterwards\n if (!before || !after) {\n throw new Error('Cannot have both before & after as null');\n }\n\n if (before.id !== after.id) {\n throw new Error('Cannot change id of a ListNode');\n }\n\n const listWithPotentialReorder = before.nextId === after.nextId\n ? list\n : list\n .remove(before.id)\n .insert(after);\n\n return listWithPotentialReorder.update(after);\n};\n"],"names":[],"mappings":";;;;AAEa,MAAA,mBAAmB,GAAG,CACjC,MAAgB,EAChB,KAAe,EACf,IAAmB,KACjB;;AAEF,IAAA,IAAI,CAAC,KAAK,IAAI,MAAM,EAAE;QACpB,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;AAC/B,KAAA;;AAGD,IAAA,IAAI,CAAC,MAAM,IAAI,KAAK,EAAE;AACpB,QAAA,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAC3B,KAAA;;;AAID,IAAA,IAAI,CAAC,MAAM,IAAI,CAAC,KAAK,EAAE;AACrB,QAAA,MAAM,IAAI,KAAK,CAAC,yCAAyC,CAAC,CAAC;AAC5D,KAAA;AAED,IAAA,IAAI,MAAM,CAAC,EAAE,KAAK,KAAK,CAAC,EAAE,EAAE;AAC1B,QAAA,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAC;AACnD,KAAA;IAED,MAAM,wBAAwB,GAAG,MAAM,CAAC,MAAM,KAAK,KAAK,CAAC,MAAM;AAC7D,UAAE,IAAI;AACN,UAAE,IAAI;AACH,aAAA,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC;aACjB,MAAM,CAAC,KAAK,CAAC,CAAC;AAEnB,IAAA,OAAO,wBAAwB,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAChD;;;;"}
|
|
@@ -30,7 +30,7 @@ const createLinkedList = (headId, items) => {
|
|
|
30
30
|
previousId: current.id,
|
|
31
31
|
format: currentNodeFormat,
|
|
32
32
|
count: newCount,
|
|
33
|
-
}
|
|
33
|
+
},
|
|
34
34
|
};
|
|
35
35
|
}, {
|
|
36
36
|
initValue: {
|
|
@@ -42,7 +42,7 @@ const createLinkedList = (headId, items) => {
|
|
|
42
42
|
items,
|
|
43
43
|
});
|
|
44
44
|
const manipulationConfig = {
|
|
45
|
-
factory: (
|
|
45
|
+
factory: (newList) => createLinkedList(newList.headId, newList.items),
|
|
46
46
|
/**
|
|
47
47
|
* At this point isDoubleLinked cannot be undefined
|
|
48
48
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-linked-list.js","sources":["../../src/linked-list/create-linked-list.ts"],"sourcesContent":["import { LinkedListItem, LinkedList, ManipulationConfig, LinkedListFormat } from './type';\n\nimport { reduceLinkedList } from './reduce-linked-list';\nimport { findLinkedList } from './find-linked-list';\nimport { whileLinkedList } from './while-linked-list';\nimport { walkListItemRecord } from './linked-list-utils';\nimport { removeLinkedList } from './remove-linked-list';\nimport { insertLinkedList } from './insert-linked-list';\nimport { getLinkedList } from './get-linked-list';\nimport { updateLinkedList } from './update-linked-list';\nimport { applyDiffLinkedList } from './apply-diff-linked-list';\n\ntype LinkedListValidation = {\n format: LinkedListFormat | null,\n previousId: null | string,\n count: number,\n}
|
|
1
|
+
{"version":3,"file":"create-linked-list.js","sources":["../../src/linked-list/create-linked-list.ts"],"sourcesContent":["import { LinkedListItem, LinkedList, ManipulationConfig, LinkedListFormat } from './type';\n\nimport { reduceLinkedList } from './reduce-linked-list';\nimport { findLinkedList } from './find-linked-list';\nimport { whileLinkedList } from './while-linked-list';\nimport { walkListItemRecord } from './linked-list-utils';\nimport { removeLinkedList } from './remove-linked-list';\nimport { insertLinkedList } from './insert-linked-list';\nimport { getLinkedList } from './get-linked-list';\nimport { updateLinkedList } from './update-linked-list';\nimport { applyDiffLinkedList } from './apply-diff-linked-list';\n\ntype LinkedListValidation = {\n format: LinkedListFormat | null,\n previousId: null | string,\n count: number,\n};\n\nexport const createLinkedList = <T extends LinkedListItem>(headId: string | null, items: Record<string, T>): LinkedList<T> => {\n const head = headId ? items[headId] : null;\n\n if (head === undefined) {\n throw new Error('Cannot find the head.');\n }\n\n const list = { headId, items };\n\n const { format, count } = walkListItemRecord((acc, current) => {\n const newCount = acc.count + 1;\n const currentNodeFormat: LinkedListFormat = current.previousId !== undefined ? 'double' : 'single';\n const incompatibleFormats = acc.format !== null && acc.format !== currentNodeFormat;\n\n if (incompatibleFormats || (currentNodeFormat === 'double' && current.previousId !== acc.previousId)) {\n throw new Error('Invalid previous node in the linked list');\n }\n\n return {\n result: {\n previousId: current.id,\n format: currentNodeFormat,\n count: newCount,\n },\n };\n }, {\n initValue: {\n format: null,\n count: 0,\n previousId: null,\n } as LinkedListValidation,\n startId: headId,\n items,\n });\n\n const manipulationConfig: ManipulationConfig<T> = {\n factory: (newList) => createLinkedList(newList.headId, newList.items),\n /**\n * At this point isDoubleLinked cannot be undefined\n */\n format,\n list,\n };\n\n const linkedList: LinkedList<T> = {\n head,\n items,\n format,\n size: count,\n reduce: (cb, initValue) => reduceLinkedList(cb, initValue, list),\n find: (predicate) => findLinkedList(predicate, list),\n while: (predicate, startId) => whileLinkedList(predicate, list, startId),\n remove: (id) => removeLinkedList(id, manipulationConfig),\n insert: (item) => insertLinkedList(item, manipulationConfig),\n get: (id) => getLinkedList(id, list),\n update: (item) => updateLinkedList(item, manipulationConfig),\n applyDiff: (before, after) => applyDiffLinkedList(before, after, linkedList),\n };\n\n return linkedList;\n};\n"],"names":["walkListItemRecord","reduceLinkedList","findLinkedList","whileLinkedList","removeLinkedList","insertLinkedList","getLinkedList","updateLinkedList","applyDiffLinkedList"],"mappings":";;;;;;;;;;;;;;MAkBa,gBAAgB,GAAG,CAA2B,MAAqB,EAAE,KAAwB,KAAmB;AAC3H,IAAA,MAAM,IAAI,GAAG,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC;IAE3C,IAAI,IAAI,KAAK,SAAS,EAAE;AACtB,QAAA,MAAM,IAAI,KAAK,CAAC,uBAAuB,CAAC,CAAC;AAC1C,KAAA;AAED,IAAA,MAAM,IAAI,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;AAE/B,IAAA,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,GAAGA,kCAAkB,CAAC,CAAC,GAAG,EAAE,OAAO,KAAI;AAC5D,QAAA,MAAM,QAAQ,GAAG,GAAG,CAAC,KAAK,GAAG,CAAC,CAAC;AAC/B,QAAA,MAAM,iBAAiB,GAAqB,OAAO,CAAC,UAAU,KAAK,SAAS,GAAG,QAAQ,GAAG,QAAQ,CAAC;AACnG,QAAA,MAAM,mBAAmB,GAAG,GAAG,CAAC,MAAM,KAAK,IAAI,IAAI,GAAG,CAAC,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,CAAC,IAAI,KAAKC,iCAAgB,CAAC,IAAI,EAAE,kBAAkB,CAAC;QAC5D,GAAG,EAAE,CAAC,EAAE,KAAKC,2BAAa,CAAC,EAAE,EAAE,IAAI,CAAC;QACpC,MAAM,EAAE,CAAC,IAAI,KAAKC,iCAAgB,CAAC,IAAI,EAAE,kBAAkB,CAAC;AAC5D,QAAA,SAAS,EAAE,CAAC,MAAM,EAAE,KAAK,KAAKC,uCAAmB,CAAC,MAAM,EAAE,KAAK,EAAE,UAAU,CAAC;KAC7E,CAAC;AAEF,IAAA,OAAO,UAAU,CAAC;AACpB;;;;"}
|
|
@@ -4,25 +4,23 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var linkedListUtils = require('./linked-list-utils.js');
|
|
6
6
|
|
|
7
|
-
const findLinkedList = (predicate, list) => {
|
|
8
|
-
|
|
9
|
-
if (predicate(item)) {
|
|
10
|
-
return {
|
|
11
|
-
result: item,
|
|
12
|
-
control: {
|
|
13
|
-
stop: true,
|
|
14
|
-
},
|
|
15
|
-
};
|
|
16
|
-
}
|
|
7
|
+
const findLinkedList = (predicate, list) => (linkedListUtils.walkListItemRecord((acc, item) => {
|
|
8
|
+
if (predicate(item)) {
|
|
17
9
|
return {
|
|
18
|
-
result:
|
|
10
|
+
result: item,
|
|
11
|
+
control: {
|
|
12
|
+
stop: true,
|
|
13
|
+
},
|
|
19
14
|
};
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
15
|
+
}
|
|
16
|
+
return {
|
|
17
|
+
result: acc,
|
|
18
|
+
};
|
|
19
|
+
}, {
|
|
20
|
+
initValue: null,
|
|
21
|
+
items: list.items,
|
|
22
|
+
startId: list.headId,
|
|
23
|
+
}));
|
|
26
24
|
|
|
27
25
|
exports.findLinkedList = findLinkedList;
|
|
28
26
|
//# sourceMappingURL=find-linked-list.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"find-linked-list.js","sources":["../../src/linked-list/find-linked-list.ts"],"sourcesContent":["import { LinkedListItem, ItemPredicate, SimpleLinkedList } from './type';\nimport { walkListItemRecord } from './linked-list-utils';\n\nexport const findLinkedList = <Item extends LinkedListItem>(\n predicate: ItemPredicate<Item>,\n list: SimpleLinkedList<Item>,\n): Item | null =>
|
|
1
|
+
{"version":3,"file":"find-linked-list.js","sources":["../../src/linked-list/find-linked-list.ts"],"sourcesContent":["import { LinkedListItem, ItemPredicate, SimpleLinkedList } from './type';\nimport { walkListItemRecord } from './linked-list-utils';\n\nexport const findLinkedList = <Item extends LinkedListItem>(\n predicate: ItemPredicate<Item>,\n list: SimpleLinkedList<Item>,\n): Item | null => (\n walkListItemRecord((acc, item) => {\n if (predicate(item)) {\n return {\n result: item,\n control: {\n stop: true,\n },\n };\n }\n\n return {\n result: acc,\n };\n }, {\n initValue: null as Item | null,\n items: list.items,\n startId: list.headId,\n })\n );\n"],"names":["walkListItemRecord"],"mappings":";;;;;;AAGa,MAAA,cAAc,GAAG,CAC5B,SAA8B,EAC9B,IAA4B,MAE1BA,kCAAkB,CAAC,CAAC,GAAG,EAAE,IAAI,KAAI;AAC/B,IAAA,IAAI,SAAS,CAAC,IAAI,CAAC,EAAE;QACnB,OAAO;AACL,YAAA,MAAM,EAAE,IAAI;AACZ,YAAA,OAAO,EAAE;AACP,gBAAA,IAAI,EAAE,IAAI;AACX,aAAA;SACF,CAAC;AACH,KAAA;IAED,OAAO;AACL,QAAA,MAAM,EAAE,GAAG;KACZ,CAAC;AACJ,CAAC,EAAE;AACD,IAAA,SAAS,EAAE,IAAmB;IAC9B,KAAK,EAAE,IAAI,CAAC,KAAK;IACjB,OAAO,EAAE,IAAI,CAAC,MAAM;AACrB,CAAA,CAAC;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"get-linked-list.js","sources":["../../src/linked-list/get-linked-list.ts"],"sourcesContent":["import { SimpleLinkedList, LinkedListItem } from './type';\n\nexport const getLinkedList = <T extends LinkedListItem>(id: string, list: SimpleLinkedList<T>) => {\n if (list.items[id]) {\n return list.items[id];\n }\n\n return null;\n}
|
|
1
|
+
{"version":3,"file":"get-linked-list.js","sources":["../../src/linked-list/get-linked-list.ts"],"sourcesContent":["import { SimpleLinkedList, LinkedListItem } from './type';\n\nexport const getLinkedList = <T extends LinkedListItem>(id: string, list: SimpleLinkedList<T>) => {\n if (list.items[id]) {\n return list.items[id];\n }\n\n return null;\n};\n"],"names":[],"mappings":";;;;MAEa,aAAa,GAAG,CAA2B,EAAU,EAAE,IAAyB,KAAI;AAC/F,IAAA,IAAI,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,EAAE;AAClB,QAAA,OAAO,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;AACvB,KAAA;AAED,IAAA,OAAO,IAAI,CAAC;AACd;;;;"}
|
|
@@ -34,11 +34,11 @@ const walkListItemRecord = (callback, config) => {
|
|
|
34
34
|
if (!currentItem) {
|
|
35
35
|
throw new Error('Cul de sac detected');
|
|
36
36
|
}
|
|
37
|
-
const { result, control } = callback(current.accumulator, currentItem);
|
|
37
|
+
const { result: currentResult, control } = callback(current.accumulator, currentItem);
|
|
38
38
|
return {
|
|
39
|
-
accumulator:
|
|
39
|
+
accumulator: currentResult,
|
|
40
40
|
currentId: currentItem.nextId,
|
|
41
|
-
control
|
|
41
|
+
control,
|
|
42
42
|
};
|
|
43
43
|
},
|
|
44
44
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"linked-list-utils.js","sources":["../../src/linked-list/linked-list-utils.ts"],"sourcesContent":["import { runWhile } from '../fp';\nimport { LinkedListFormat, LinkedListItem, SimpleLinkedList } from './type';\n\nexport type WalkConfig<T extends LinkedListItem, Result> = {\n startId: string | null,\n items: Record<string, T>,\n initValue: Result,\n}
|
|
1
|
+
{"version":3,"file":"linked-list-utils.js","sources":["../../src/linked-list/linked-list-utils.ts"],"sourcesContent":["import { runWhile } from '../fp';\nimport { LinkedListFormat, LinkedListItem, SimpleLinkedList } from './type';\n\nexport type WalkConfig<T extends LinkedListItem, Result> = {\n startId: string | null,\n items: Record<string, T>,\n initValue: Result,\n};\n\ntype WalkCallbackResult<R> = {\n result: R,\n control?: {\n stop?: boolean,\n },\n};\n\nexport type WalkCallback<Item, Result> = (accumulator: Result, current: Item) => WalkCallbackResult<Result>;\n\ntype RunWhileValue<T> = {\n currentId: string | null,\n accumulator: T,\n control: WalkCallbackResult<any>['control'],\n};\n\nexport const walkListItemRecord = <T extends LinkedListItem, Result>(\n callback: WalkCallback<T, Result>,\n config: WalkConfig<T, Result>,\n) => {\n const { items, startId, initValue } = config;\n const visited = new Set();\n const totalNodes = Object.keys(items).length;\n\n const { result } = runWhile({\n iterationLimit: totalNodes + 1,\n initialValue: {\n currentId: startId,\n accumulator: initValue,\n control: {\n stop: false,\n },\n } as RunWhileValue<Result>,\n predicate: ({ control, currentId }) => {\n const stop = control?.stop;\n const forcedStop = stop === undefined ? false : stop;\n\n /**\n * Stop early or when the end is reached\n */\n return !forcedStop && currentId !== null;\n },\n callback: (current) => {\n if (visited.has(current.currentId)) {\n throw new Error('Infinite loop found');\n }\n\n visited.add(current.currentId);\n\n const currentItem = items[current.currentId!];\n\n if (!currentItem) {\n throw new Error('Cul de sac detected');\n }\n\n const { result: currentResult, control } = callback(current.accumulator, currentItem);\n\n return {\n accumulator: currentResult,\n currentId: currentItem.nextId,\n control,\n };\n },\n });\n\n return result.accumulator;\n};\n\nexport const findPreviousId = <T extends LinkedListItem>(id: string | null, list: SimpleLinkedList<T>) => {\n if (id === list.headId) {\n return null;\n }\n\n return walkListItemRecord(\n (acc, current) => {\n if (current.nextId === id) {\n return {\n result: current.id,\n control: {\n stop: true,\n },\n };\n }\n\n return {\n result: acc,\n };\n },\n {\n initValue: null as null | string,\n startId: list.headId,\n items: list.items,\n },\n );\n};\n\nexport const assertItemCompatibility = <T extends LinkedListItem>(item: T, format: LinkedListFormat | null) => {\n /**\n * An empty linked list won't have any format yet, it will be\n * determined by the first item inserted in the list.\n */\n if (format === null) {\n return;\n }\n\n const incompatibleWithDoubleLinks = item.previousId === undefined && format === 'double';\n const incompatibleWithSingleLink = item.previousId !== undefined && format === 'single';\n\n if (incompatibleWithDoubleLinks || incompatibleWithSingleLink) {\n throw new Error(`Node is incompatible with the list (id: ${item.id})`);\n }\n};\n\nexport const assertPresence = (id: string | null | undefined, items: Record<string, unknown>) => {\n if (id && !items[id]) {\n throw new Error(`Node not found in the list (id: ${id})`);\n }\n};\n\ntype PositionAssertionConfig = {\n previousId: string | null,\n list: SimpleLinkedList<LinkedListItem>,\n};\n\nexport const assertNewNodePosition = <T extends LinkedListItem>(item: T, config: PositionAssertionConfig) => {\n const { previousId, list } = config;\n\n const insertionMiddleError = previousId && list.items[previousId].nextId !== item.nextId;\n const insertionTailError = previousId && item.nextId === null && list.items[previousId].nextId !== null;\n const insertionHeadError = previousId === null && item.nextId !== list.headId;\n\n if (insertionMiddleError || insertionTailError || insertionHeadError) {\n throw new Error(`Invalid position for given node (id: ${item.id})`);\n }\n};\n"],"names":["runWhile"],"mappings":";;;;;;MAwBa,kBAAkB,GAAG,CAChC,QAAiC,EACjC,MAA6B,KAC3B;IACF,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,GAAG,MAAM,CAAC;AAC7C,IAAA,MAAM,OAAO,GAAG,IAAI,GAAG,EAAE,CAAC;IAC1B,MAAM,UAAU,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC;AAE7C,IAAA,MAAM,EAAE,MAAM,EAAE,GAAGA,iBAAQ,CAAC;QAC1B,cAAc,EAAE,UAAU,GAAG,CAAC;AAC9B,QAAA,YAAY,EAAE;AACZ,YAAA,SAAS,EAAE,OAAO;AAClB,YAAA,WAAW,EAAE,SAAS;AACtB,YAAA,OAAO,EAAE;AACP,gBAAA,IAAI,EAAE,KAAK;AACZ,aAAA;AACuB,S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M,CAAC;AAEpC,IAAA,MAAM,oBAAoB,GAAG,UAAU,IAAI,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,MAAM,KAAK,IAAI,CAAC,MAAM,CAAC;IACzF,MAAM,kBAAkB,GAAG,UAAU,IAAI,IAAI,CAAC,MAAM,KAAK,IAAI,IAAI,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,MAAM,KAAK,IAAI,CAAC;AACxG,IAAA,MAAM,kBAAkB,GAAG,UAAU,KAAK,IAAI,IAAI,IAAI,CAAC,MAAM,KAAK,IAAI,CAAC,MAAM,CAAC;AAE9E,IAAA,IAAI,oBAAoB,IAAI,kBAAkB,IAAI,kBAAkB,EAAE;QACpE,MAAM,IAAI,KAAK,CAAC,CAAA,qCAAA,EAAwC,IAAI,CAAC,EAAE,CAAG,CAAA,CAAA,CAAC,CAAC;AACrE,KAAA;AACH;;;;;;;;"}
|
|
@@ -6,11 +6,9 @@ var linkedListUtils = require('./linked-list-utils.js');
|
|
|
6
6
|
|
|
7
7
|
const reduceLinkedList = (cb, initValue, list) => {
|
|
8
8
|
const { items, headId } = list;
|
|
9
|
-
return linkedListUtils.walkListItemRecord((acc, current) => {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
};
|
|
13
|
-
}, {
|
|
9
|
+
return linkedListUtils.walkListItemRecord((acc, current) => ({
|
|
10
|
+
result: cb(acc, current),
|
|
11
|
+
}), {
|
|
14
12
|
startId: headId,
|
|
15
13
|
items,
|
|
16
14
|
initValue,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"reduce-linked-list.js","sources":["../../src/linked-list/reduce-linked-list.ts"],"sourcesContent":["import { ReduceCallback, LinkedListItem, SimpleLinkedList } from './type';\nimport { walkListItemRecord } from './linked-list-utils';\n\nexport const reduceLinkedList = <Item extends LinkedListItem, Result>(\n cb: ReduceCallback<Item, Result>,\n initValue: Result,\n list: SimpleLinkedList<Item>,\n): Result => {\n const { items, headId } = list;\n\n return walkListItemRecord((acc, current) => {\n
|
|
1
|
+
{"version":3,"file":"reduce-linked-list.js","sources":["../../src/linked-list/reduce-linked-list.ts"],"sourcesContent":["import { ReduceCallback, LinkedListItem, SimpleLinkedList } from './type';\nimport { walkListItemRecord } from './linked-list-utils';\n\nexport const reduceLinkedList = <Item extends LinkedListItem, Result>(\n cb: ReduceCallback<Item, Result>,\n initValue: Result,\n list: SimpleLinkedList<Item>,\n): Result => {\n const { items, headId } = list;\n\n return walkListItemRecord((acc, current) => ({\n result: cb(acc, current),\n }), {\n startId: headId,\n items,\n initValue,\n });\n};\n"],"names":["walkListItemRecord"],"mappings":";;;;;;AAGa,MAAA,gBAAgB,GAAG,CAC9B,EAAgC,EAChC,SAAiB,EACjB,IAA4B,KAClB;AACV,IAAA,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;IAE/B,OAAOA,kCAAkB,CAAC,CAAC,GAAG,EAAE,OAAO,MAAM;AAC3C,QAAA,MAAM,EAAE,EAAE,CAAC,GAAG,EAAE,OAAO,CAAC;AACzB,KAAA,CAAC,EAAE;AACF,QAAA,OAAO,EAAE,MAAM;QACf,KAAK;QACL,SAAS;AACV,KAAA,CAAC,CAAC;AACL;;;;"}
|
|
@@ -17,7 +17,7 @@ const removeLinkedList = (id, config) => {
|
|
|
17
17
|
const newItems = Object.assign(Object.assign(Object.assign({}, otherItems), (previousId ? {
|
|
18
18
|
[previousId]: Object.assign(Object.assign({}, otherItems[previousId]), { nextId: itemToDelete.nextId }),
|
|
19
19
|
} : {})), (isDoubleLinkList && itemToDelete.nextId ? {
|
|
20
|
-
[itemToDelete.nextId]: Object.assign(Object.assign({}, otherItems[itemToDelete.nextId]), (isDoubleLinkList ? { previousId } : {}))
|
|
20
|
+
[itemToDelete.nextId]: Object.assign(Object.assign({}, otherItems[itemToDelete.nextId]), (isDoubleLinkList ? { previousId } : {})),
|
|
21
21
|
} : {}));
|
|
22
22
|
return factory({
|
|
23
23
|
headId: newHeadId,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"remove-linked-list.js","sources":["../../src/linked-list/remove-linked-list.ts"],"sourcesContent":["import { ManipulationConfig, LinkedListItem } from './type';\nimport { assertPresence, findPreviousId } from './linked-list-utils';\n\nexport const removeLinkedList = <T extends LinkedListItem>(id: string, config: ManipulationConfig<T>) => {\n const { format, list, factory } = config;\n const { [id]: itemToDelete, ...otherItems } = list.items;\n\n assertPresence(id, list.items);\n\n const isDoubleLinkList = format === 'double';\n const previousId = isDoubleLinkList\n ? itemToDelete.previousId!\n : findPreviousId(id, list);\n\n const newHeadId = previousId === null ? itemToDelete.nextId : list.headId;\n const newItems = {\n ...otherItems,\n ...(previousId ? {\n [previousId]: {\n ...otherItems[previousId],\n nextId: itemToDelete.nextId,\n },\n } : {}),\n ...(isDoubleLinkList && itemToDelete.nextId ? {\n [itemToDelete.nextId]: {\n ...otherItems[itemToDelete.nextId],\n ...(isDoubleLinkList ? { previousId } : {}),\n }
|
|
1
|
+
{"version":3,"file":"remove-linked-list.js","sources":["../../src/linked-list/remove-linked-list.ts"],"sourcesContent":["import { ManipulationConfig, LinkedListItem } from './type';\nimport { assertPresence, findPreviousId } from './linked-list-utils';\n\nexport const removeLinkedList = <T extends LinkedListItem>(id: string, config: ManipulationConfig<T>) => {\n const { format, list, factory } = config;\n const { [id]: itemToDelete, ...otherItems } = list.items;\n\n assertPresence(id, list.items);\n\n const isDoubleLinkList = format === 'double';\n const previousId = isDoubleLinkList\n ? itemToDelete.previousId!\n : findPreviousId(id, list);\n\n const newHeadId = previousId === null ? itemToDelete.nextId : list.headId;\n const newItems = {\n ...otherItems,\n ...(previousId ? {\n [previousId]: {\n ...otherItems[previousId],\n nextId: itemToDelete.nextId,\n },\n } : {}),\n ...(isDoubleLinkList && itemToDelete.nextId ? {\n [itemToDelete.nextId]: {\n ...otherItems[itemToDelete.nextId],\n ...(isDoubleLinkList ? { previousId } : {}),\n },\n } : {}),\n };\n\n return factory({\n headId: newH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UAAU,EAAE,GAAG,EAAE,EAC3C;AACF,KAAA,GAAG,EAAE,EACP,CAAC;AAEF,IAAA,OAAO,OAAO,CAAC;AACb,QAAA,MAAM,EAAE,SAAS;AACjB,QAAA,KAAK,EAAE,QAAQ;AAChB,KAAA,CAAC,CAAC;AACL;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"update-linked-list.js","sources":["../../src/linked-list/update-linked-list.ts"],"sourcesContent":["import { ManipulationConfig, LinkedListItem } from './type';\nimport { assertPresence } from './linked-list-utils';\n\nexport const updateLinkedList = <T extends LinkedListItem>(item: T,
|
|
1
|
+
{"version":3,"file":"update-linked-list.js","sources":["../../src/linked-list/update-linked-list.ts"],"sourcesContent":["import { ManipulationConfig, LinkedListItem } from './type';\nimport { assertPresence } from './linked-list-utils';\n\nexport const updateLinkedList = <T extends LinkedListItem>(item: T, config: ManipulationConfig<T>) => {\n const { list, factory } = config;\n\n assertPresence(item.id, list.items);\n\n /**\n * This method should not update any ids.\n */\n const { nextId, previousId, id, ...updatedProperties } = item;\n const currentItem = list.items[item.id];\n\n const newItems = {\n ...list.items,\n [item.id]: {\n nextId: currentItem.nextId,\n previousId: currentItem.previousId,\n id: currentItem.id,\n ...updatedProperties,\n } as T,\n };\n\n return factory({\n headId: list.headId,\n items: newItems,\n });\n};\n"],"names":["assertPresence","__rest"],"mappings":";;;;;;;MAGa,gBAAgB,GAAG,CAA2B,IAAO,EAAE,MAA6B,KAAI;AACnG,IAAA,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,MAAM,CAAC;IAEjCA,8BAAc,CAAC,IAAI,CAAC,EAAE,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC;AAEpC;;AAEG;AACH,IAAA,MAAmC,iBAAiB,GAAKC,aAAA,CAAA,IAAI,EAAvD,CAAA,QAAA,EAAA,YAAA,EAAA,IAAA,CAAgD,EAAQ;IAC9D,MAAM,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AAExC,IAAA,MAAM,QAAQ,GAAA,MAAA,CAAA,MAAA,CAAA,MAAA,CAAA,MAAA,CAAA,EAAA,EACT,IAAI,CAAC,KAAK,CACb,EAAA,EAAA,CAAC,IAAI,CAAC,EAAE,GAAG,MACT,CAAA,MAAA,CAAA,EAAA,MAAM,EAAE,WAAW,CAAC,MAAM,EAC1B,UAAU,EAAE,WAAW,CAAC,UAAU,EAClC,EAAE,EAAE,WAAW,CAAC,EAAE,EAAA,EACf,iBAAiB,CAChB,GACP,CAAC;AAEF,IAAA,OAAO,OAAO,CAAC;QACb,MAAM,EAAE,IAAI,CAAC,MAAM;AACnB,QAAA,KAAK,EAAE,QAAQ;AAChB,KAAA,CAAC,CAAC;AACL;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"while-linked-list.js","sources":["../../src/linked-list/while-linked-list.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"while-linked-list.js","sources":["../../src/linked-list/while-linked-list.ts"],"sourcesContent":["import { LinkedListItem, ItemPredicate, SimpleLinkedList } from './type';\nimport { walkListItemRecord } from './linked-list-utils';\n\nexport const whileLinkedList = <Item extends LinkedListItem>(\n predicate: ItemPredicate<Item>,\n list: SimpleLinkedList<Item>,\n startId?: string,\n): string[] => {\n if (startId && !list.items[startId]) {\n throw new Error('Cannot start from a node that does not exist');\n }\n\n return walkListItemRecord((acc, item) => {\n const itemMatched = predicate(item);\n\n if (itemMatched) {\n return {\n result: [...acc, item.id],\n };\n }\n\n return {\n result: acc,\n control: {\n stop: true,\n },\n };\n }, {\n initValue: [] as string[],\n startId: startId || list.headId,\n items: list.items,\n });\n};\n"],"names":["walkListItemRecord"],"mappings":";;;;;;AAGa,MAAA,eAAe,GAAG,CAC7B,SAA8B,EAC9B,IAA4B,EAC5B,OAAgB,KACJ;IACZ,IAAI,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE;AACnC,QAAA,MAAM,IAAI,KAAK,CAAC,8CAA8C,CAAC,CAAC;AACjE,KAAA;AAED,IAAA,OAAOA,kCAAkB,CAAC,CAAC,GAAG,EAAE,IAAI,KAAI;AACtC,QAAA,MAAM,WAAW,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC;AAEpC,QAAA,IAAI,WAAW,EAAE;YACf,OAAO;gBACL,MAAM,EAAE,CAAC,GAAG,GAAG,EAAE,IAAI,CAAC,EAAE,CAAC;aAC1B,CAAC;AACH,SAAA;QAED,OAAO;AACL,YAAA,MAAM,EAAE,GAAG;AACX,YAAA,OAAO,EAAE;AACP,gBAAA,IAAI,EAAE,IAAI;AACX,aAAA;SACF,CAAC;AACJ,KAAC,EAAE;AACD,QAAA,SAAS,EAAE,EAAc;AACzB,QAAA,OAAO,EAAE,OAAO,IAAI,IAAI,CAAC,MAAM;QAC/B,KAAK,EAAE,IAAI,CAAC,KAAK;AAClB,KAAA,CAAC,CAAC;AACL;;;;"}
|
package/math/clamp-number.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"clamp-number.js","sources":["../../src/math/clamp-number.ts"],"sourcesContent":["import { assertNumberRange } from './number-range';\nimport type { NumberRange } from './number-range';\n\nexport const clampNumber = (num: number, { min, max }: NumberRange) => {\n assertNumberRange({ min, max });\n\n return Math.min(Math.max(num, min), max);\n}
|
|
1
|
+
{"version":3,"file":"clamp-number.js","sources":["../../src/math/clamp-number.ts"],"sourcesContent":["import { assertNumberRange } from './number-range';\nimport type { NumberRange } from './number-range';\n\nexport const clampNumber = (num: number, { min, max }: NumberRange) => {\n assertNumberRange({ min, max });\n\n return Math.min(Math.max(num, min), max);\n};\n"],"names":["assertNumberRange"],"mappings":";;;;;;AAGO,MAAM,WAAW,GAAG,CAAC,GAAW,EAAE,EAAE,GAAG,EAAE,GAAG,EAAe,KAAI;AACpE,IAAAA,6BAAiB,CAAC,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC;AAEhC,IAAA,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE,GAAG,CAAC,CAAC;AAC3C;;;;"}
|
package/math/interpolate.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interpolate.js","sources":["../../src/math/interpolate.ts"],"sourcesContent":["import { isNumberInRange } from './number-range';\n\nexport type RangeArray = [number, number];\n\nexport const interpolate = (num: number, inputRange: RangeArray, outputRange: RangeArray) => {\n const [x1, x2] = inputRange;\n const [y1, y2] = outputRange;\n\n if (x1 > x2) {\n throw new Error('interpolate: inputRange[0] should not be superior to inputRange[1]');\n }\n\n if (!isNumberInRange(num, { min: x1, max: x2 })) {\n throw new Error(`interpolate: input number {${num}} is not in range of {${x1}}-{${x2}}`);\n }\n\n return y1 + ((num - x1) / (x2 - x1)) * (y2 - y1);\n}
|
|
1
|
+
{"version":3,"file":"interpolate.js","sources":["../../src/math/interpolate.ts"],"sourcesContent":["import { isNumberInRange } from './number-range';\n\nexport type RangeArray = [number, number];\n\nexport const interpolate = (num: number, inputRange: RangeArray, outputRange: RangeArray) => {\n const [x1, x2] = inputRange;\n const [y1, y2] = outputRange;\n\n if (x1 > x2) {\n throw new Error('interpolate: inputRange[0] should not be superior to inputRange[1]');\n }\n\n if (!isNumberInRange(num, { min: x1, max: x2 })) {\n throw new Error(`interpolate: input number {${num}} is not in range of {${x1}}-{${x2}}`);\n }\n\n return y1 + ((num - x1) / (x2 - x1)) * (y2 - y1);\n};\n"],"names":["isNumberInRange"],"mappings":";;;;;;AAIa,MAAA,WAAW,GAAG,CAAC,GAAW,EAAE,UAAsB,EAAE,WAAuB,KAAI;AAC1F,IAAA,MAAM,CAAC,EAAE,EAAE,EAAE,CAAC,GAAG,UAAU,CAAC;AAC5B,IAAA,MAAM,CAAC,EAAE,EAAE,EAAE,CAAC,GAAG,WAAW,CAAC;IAE7B,IAAI,EAAE,GAAG,EAAE,EAAE;AACX,QAAA,MAAM,IAAI,KAAK,CAAC,oEAAoE,CAAC,CAAC;AACvF,KAAA;AAED,IAAA,IAAI,CAACA,2BAAe,CAAC,GAAG,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,CAAC,EAAE;QAC/C,MAAM,IAAI,KAAK,CAAC,CAA8B,2BAAA,EAAA,GAAG,CAAyB,sBAAA,EAAA,EAAE,CAAM,GAAA,EAAA,EAAE,CAAG,CAAA,CAAA,CAAC,CAAC;AAC1F,KAAA;IAED,OAAO,EAAE,GAAG,CAAC,CAAC,GAAG,GAAG,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE,CAAC,CAAC;AACnD;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sparted/shared-library",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"repository": "git@github.com:Sparted/shared-library.git",
|
|
5
5
|
"author": "Thomas <duprethomasfr@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -13,13 +13,24 @@
|
|
|
13
13
|
],
|
|
14
14
|
"scripts": {
|
|
15
15
|
"test": "jest",
|
|
16
|
+
"lint": "eslint .",
|
|
16
17
|
"clean": "rm -rf ./dist",
|
|
17
18
|
"build": "yarn clean && rollup --config rollup.config.ts --configPlugin typescript2 && cp package.json ./dist/package.json",
|
|
18
19
|
"build-and-publish": "yarn test && yarn build && (cd ./dist && NPM_CONFIG_REGISTRY=undefined npm publish)"
|
|
19
20
|
},
|
|
20
21
|
"devDependencies": {
|
|
22
|
+
"@sparted/eslint-config": "^1.0.0",
|
|
21
23
|
"@types/glob": "^8.0.0",
|
|
22
24
|
"@types/jest": "^29.0.3",
|
|
25
|
+
"@typescript-eslint/eslint-plugin": "^5.8.0",
|
|
26
|
+
"@typescript-eslint/parser": "^5.8.0",
|
|
27
|
+
"eslint": "^7.32.0",
|
|
28
|
+
"eslint-config-airbnb": "^19.0.4",
|
|
29
|
+
"eslint-config-airbnb-typescript": "^16.1.0",
|
|
30
|
+
"eslint-plugin-import": "^2.25.3",
|
|
31
|
+
"eslint-plugin-jsx-a11y": "^6.5.1",
|
|
32
|
+
"eslint-plugin-react": "^7.28.0",
|
|
33
|
+
"eslint-plugin-react-hooks": "^4.3.0",
|
|
23
34
|
"glob": "^8.0.3",
|
|
24
35
|
"jest": "^29.0.3",
|
|
25
36
|
"jest-environment-jsdom": "^29.0.3",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse-boolean.js","sources":["../../src/parsing/parse-boolean.ts"],"sourcesContent":["/**\n * Parse a string to a boolean. Throw error if string is neither \"true\" nor \"false\"\n */\
|
|
1
|
+
{"version":3,"file":"parse-boolean.js","sources":["../../src/parsing/parse-boolean.ts"],"sourcesContent":["/**\n * Parse a string to a boolean. Throw error if string is neither \"true\" nor \"false\"\n */\nexport const parseBoolean = (value: string): boolean => {\n if (value !== 'true' && value !== 'false') {\n throw new Error('Cannot parse value');\n }\n\n return value === 'true';\n};\n"],"names":[],"mappings":";;;;AAAA;;AAEG;AACU,MAAA,YAAY,GAAG,CAAC,KAAa,KAAa;AACrD,IAAA,IAAI,KAAK,KAAK,MAAM,IAAI,KAAK,KAAK,OAAO,EAAE;AACzC,QAAA,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAC;AACvC,KAAA;IAED,OAAO,KAAK,KAAK,MAAM,CAAC;AAC1B;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse-enum.js","sources":["../../src/parsing/parse-enum.ts"],"sourcesContent":["import type { Primitive } from '../types';\n\n/**\n * Factory to build enum to parse a string to an enum. Throw error if string is not an enum value\n */\
|
|
1
|
+
{"version":3,"file":"parse-enum.js","sources":["../../src/parsing/parse-enum.ts"],"sourcesContent":["import type { Primitive } from '../types';\n\n/**\n * Factory to build enum to parse a string to an enum. Throw error if string is not an enum value\n */\nexport const createEnumParser = <E extends Record<string, Primitive>>(enumType: E) => (\n (value: string) => {\n const enumValues = Object.values(enumType);\n\n if (!enumValues.includes(value as any)) {\n throw new Error('Can\\'t parse this value');\n }\n\n return value as unknown as E;\n }\n);\n"],"names":[],"mappings":";;;;AAEA;;AAEG;AACI,MAAM,gBAAgB,GAAG,CAAsC,QAAW,MAC/E,CAAC,KAAa,KAAI;IAChB,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AAE3C,IAAA,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,KAAY,CAAC,EAAE;AACtC,QAAA,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;AAC5C,KAAA;AAED,IAAA,OAAO,KAAqB,CAAC;AAC/B,CAAC;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse-number.js","sources":["../../src/parsing/parse-number.ts"],"sourcesContent":["/**\n * Parse a string to a number. Throw error if string is NaN\n */\
|
|
1
|
+
{"version":3,"file":"parse-number.js","sources":["../../src/parsing/parse-number.ts"],"sourcesContent":["/**\n * Parse a string to a number. Throw error if string is NaN\n */\nexport const parseNumber = (value: string): number => {\n const number = parseInt(value, 10);\n\n if (Number.isNaN(number)) {\n throw new Error('Cannot parse value');\n }\n\n return number;\n};\n"],"names":[],"mappings":";;;;AAAA;;AAEG;AACU,MAAA,WAAW,GAAG,CAAC,KAAa,KAAY;IACnD,MAAM,MAAM,GAAG,QAAQ,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;AAEnC,IAAA,IAAI,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE;AACxB,QAAA,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAC;AACvC,KAAA;AAED,IAAA,OAAO,MAAM,CAAC;AAChB;;;;"}
|
|
@@ -2,9 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
const isFunction = (value) =>
|
|
6
|
-
return typeof value === 'function';
|
|
7
|
-
};
|
|
5
|
+
const isFunction = (value) => (typeof value === 'function');
|
|
8
6
|
|
|
9
7
|
exports.isFunction = isFunction;
|
|
10
8
|
//# sourceMappingURL=is-function.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"is-function.js","sources":["../../src/predicates/is-function.ts"],"sourcesContent":["export const isFunction = (value: any): value is Function =>
|
|
1
|
+
{"version":3,"file":"is-function.js","sources":["../../src/predicates/is-function.ts"],"sourcesContent":["export const isFunction = (value: any): value is Function => (\n typeof value === 'function'\n);\n"],"names":[],"mappings":";;;;AAAO,MAAM,UAAU,GAAG,CAAC,KAAU,MACnC,OAAO,KAAK,KAAK,UAAU;;;;"}
|
package/predicates/is-null.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"is-null.js","sources":["../../src/predicates/is-null.ts"],"sourcesContent":["export const isNull = (value: any): value is null =>
|
|
1
|
+
{"version":3,"file":"is-null.js","sources":["../../src/predicates/is-null.ts"],"sourcesContent":["export const isNull = (value: any): value is null => (\n value === null\n);\n"],"names":[],"mappings":";;;;AAAO,MAAM,MAAM,GAAG,CAAC,KAAU,MAC/B,KAAK,KAAK,IAAI;;;;"}
|
package/predicates/is-object.js
CHANGED
|
@@ -2,9 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
const isObject = (value) =>
|
|
6
|
-
return typeof value === 'object';
|
|
7
|
-
};
|
|
5
|
+
const isObject = (value) => (typeof value === 'object');
|
|
8
6
|
|
|
9
7
|
exports.isObject = isObject;
|
|
10
8
|
//# sourceMappingURL=is-object.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"is-object.js","sources":["../../src/predicates/is-object.ts"],"sourcesContent":["export const isObject = (value: any): value is Object =>
|
|
1
|
+
{"version":3,"file":"is-object.js","sources":["../../src/predicates/is-object.ts"],"sourcesContent":["export const isObject = (value: any): value is Object => (\n typeof value === 'object'\n);\n"],"names":[],"mappings":";;;;AAAO,MAAM,QAAQ,GAAG,CAAC,KAAU,MACjC,OAAO,KAAK,KAAK,QAAQ;;;;"}
|
|
@@ -7,9 +7,7 @@ const PRIMITIVE_TYPES = [
|
|
|
7
7
|
'boolean',
|
|
8
8
|
'number',
|
|
9
9
|
];
|
|
10
|
-
const isPrimitive = (value) =>
|
|
11
|
-
return PRIMITIVE_TYPES.includes(typeof value);
|
|
12
|
-
};
|
|
10
|
+
const isPrimitive = (value) => (PRIMITIVE_TYPES.includes(typeof value));
|
|
13
11
|
const isArrayOfPrimitive = (value) => {
|
|
14
12
|
if (!Array.isArray(value)) {
|
|
15
13
|
return false;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"is-primitive.js","sources":["../../src/predicates/is-primitive.ts"],"sourcesContent":["import { Primitive } from '../types/general';\n\nconst PRIMITIVE_TYPES = [\n 'string',\n 'boolean',\n 'number',\n];\n\nexport const isPrimitive = (value: any): value is Primitive =>
|
|
1
|
+
{"version":3,"file":"is-primitive.js","sources":["../../src/predicates/is-primitive.ts"],"sourcesContent":["import { Primitive } from '../types/general';\n\nconst PRIMITIVE_TYPES = [\n 'string',\n 'boolean',\n 'number',\n];\n\nexport const isPrimitive = (value: any): value is Primitive => (\n PRIMITIVE_TYPES.includes(typeof value)\n);\n\nexport const isArrayOfPrimitive = (value: any): value is Array<Primitive> => {\n if (!Array.isArray(value)) {\n return false;\n }\n\n return value.every((arrayValue) => isPrimitive(arrayValue));\n};\n"],"names":[],"mappings":";;;;AAEA,MAAM,eAAe,GAAG;IACtB,QAAQ;IACR,SAAS;IACT,QAAQ;CACT,CAAC;AAEW,MAAA,WAAW,GAAG,CAAC,KAAU,MACpC,eAAe,CAAC,QAAQ,CAAC,OAAO,KAAK,CAAC,EACtC;AAEW,MAAA,kBAAkB,GAAG,CAAC,KAAU,KAA+B;AAC1E,IAAA,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE;AACzB,QAAA,OAAO,KAAK,CAAC;AACd,KAAA;AAED,IAAA,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC,UAAU,KAAK,WAAW,CAAC,UAAU,CAAC,CAAC,CAAC;AAC9D;;;;;"}
|
|
@@ -2,9 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
const isUndefined = (value) =>
|
|
6
|
-
return value === undefined;
|
|
7
|
-
};
|
|
5
|
+
const isUndefined = (value) => (value === undefined);
|
|
8
6
|
|
|
9
7
|
exports.isUndefined = isUndefined;
|
|
10
8
|
//# sourceMappingURL=is-undefined.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"is-undefined.js","sources":["../../src/predicates/is-undefined.ts"],"sourcesContent":["export const isUndefined = (value: any): value is undefined =>
|
|
1
|
+
{"version":3,"file":"is-undefined.js","sources":["../../src/predicates/is-undefined.ts"],"sourcesContent":["export const isUndefined = (value: any): value is undefined => (\n value === undefined\n);\n"],"names":[],"mappings":";;;;AAAO,MAAM,WAAW,GAAG,CAAC,KAAU,MACpC,KAAK,KAAK,SAAS;;;;"}
|
package/time/constants.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.js","sources":["../../src/time/constants.ts"],"sourcesContent":["export const MS_SECOND = 1000;\nexport const MS_MINUTE = MS_SECOND * 60;\nexport const MS_HOUR = MS_MINUTE * 60;\nexport const MS_DAY = MS_HOUR * 24
|
|
1
|
+
{"version":3,"file":"constants.js","sources":["../../src/time/constants.ts"],"sourcesContent":["export const MS_SECOND = 1000;\nexport const MS_MINUTE = MS_SECOND * 60;\nexport const MS_HOUR = MS_MINUTE * 60;\nexport const MS_DAY = MS_HOUR * 24;\n"],"names":[],"mappings":";;;;AAAO,MAAM,SAAS,GAAG,KAAK;AACjB,MAAA,SAAS,GAAG,SAAS,GAAG,GAAG;AAC3B,MAAA,OAAO,GAAG,SAAS,GAAG,GAAG;AACzB,MAAA,MAAM,GAAG,OAAO,GAAG;;;;;;;"}
|
package/time/convert.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"convert.js","sources":["../../src/time/convert.ts"],"sourcesContent":["import { MS_MINUTE, MS_HOUR } from './constants';\n\nexport const msToMinute = (ms: number) => Math.floor(ms / MS_MINUTE);\n\nexport const msToHour = (ms: number) => Math.floor(ms / MS_HOUR)
|
|
1
|
+
{"version":3,"file":"convert.js","sources":["../../src/time/convert.ts"],"sourcesContent":["import { MS_MINUTE, MS_HOUR } from './constants';\n\nexport const msToMinute = (ms: number) => Math.floor(ms / MS_MINUTE);\n\nexport const msToHour = (ms: number) => Math.floor(ms / MS_HOUR);\n"],"names":["MS_MINUTE","MS_HOUR"],"mappings":";;;;;;AAEa,MAAA,UAAU,GAAG,CAAC,EAAU,KAAK,IAAI,CAAC,KAAK,CAAC,EAAE,GAAGA,mBAAS,EAAE;AAExD,MAAA,QAAQ,GAAG,CAAC,EAAU,KAAK,IAAI,CAAC,KAAK,CAAC,EAAE,GAAGC,iBAAO;;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"get-time-diff-as-string.js","sources":["../../src/time/get-time-diff-as-string.ts"],"sourcesContent":["import { ReplaceConfig, replaceInTemplate } from '../string';\nimport { MS_HOUR, MS_DAY } from './constants';\nimport { msToMinute, msToHour } from './convert';\n\nexport type MonthLocaleLabels = [\n string,\n string,\n string,\n string,\n string,\n string,\n string,\n string,\n string,\n string,\n string,\n string,\n];\n\nexport type DiffTemplate = {\n /**\n * e.g.: (\"%time% %timelabel% blabla\")\n */\n timeTemplate: string,\n /**\n * e.g.: \"%date% blabla\"\n */\n monthLocaleLabels: MonthLocaleLabels,\n fullDateTemplate: string,\n timeLabel: {\n minute: {\n singular: string,\n plural: string,\n },\n hour: {\n singular: string,\n plural: string,\n },\n }\n};\n\nconst defaultLocaleLabels: MonthLocaleLabels = [\n 'January',\n 'February',\n 'March',\n 'April',\n 'May',\n 'June',\n 'July',\n 'August',\n 'September',\n 'October',\n 'November',\n 'December',\n];\n\nconst defaultEnglishTemplate: DiffTemplate = {\n timeTemplate: '%time% %timeLabel% ago',\n fullDateTemplate: 'on %date%',\n monthLocaleLabels: defaultLocaleLabels,\n timeLabel: {\n minute: {\n singular: 'min',\n plural: 'min',\n },\n hour: {\n singular: 'hour',\n plural: 'hours',\n },\n },\n};\n\nconst TEMPLATE_CONFIG: ReplaceConfig = {\n delimiter: {\n left: '%',\n right: '%',\n },\n};\n\ntype HourFormat = {\n time: string,\n period: 'am' | 'pm';\n}
|
|
1
|
+
{"version":3,"file":"get-time-diff-as-string.js","sources":["../../src/time/get-time-diff-as-string.ts"],"sourcesContent":["import { ReplaceConfig, replaceInTemplate } from '../string';\nimport { MS_HOUR, MS_DAY } from './constants';\nimport { msToMinute, msToHour } from './convert';\n\nexport type MonthLocaleLabels = [\n string,\n string,\n string,\n string,\n string,\n string,\n string,\n string,\n string,\n string,\n string,\n string,\n];\n\nexport type DiffTemplate = {\n /**\n * e.g.: (\"%time% %timelabel% blabla\")\n */\n timeTemplate: string,\n /**\n * e.g.: \"%date% blabla\"\n */\n monthLocaleLabels: MonthLocaleLabels,\n fullDateTemplate: string,\n timeLabel: {\n minute: {\n singular: string,\n plural: string,\n },\n hour: {\n singular: string,\n plural: string,\n },\n }\n};\n\nconst defaultLocaleLabels: MonthLocaleLabels = [\n 'January',\n 'February',\n 'March',\n 'April',\n 'May',\n 'June',\n 'July',\n 'August',\n 'September',\n 'October',\n 'November',\n 'December',\n];\n\nconst defaultEnglishTemplate: DiffTemplate = {\n timeTemplate: '%time% %timeLabel% ago',\n fullDateTemplate: 'on %date%',\n monthLocaleLabels: defaultLocaleLabels,\n timeLabel: {\n minute: {\n singular: 'min',\n plural: 'min',\n },\n hour: {\n singular: 'hour',\n plural: 'hours',\n },\n },\n};\n\nconst TEMPLATE_CONFIG: ReplaceConfig = {\n delimiter: {\n left: '%',\n right: '%',\n },\n};\n\ntype HourFormat = {\n time: string,\n period: 'am' | 'pm';\n};\n\ntype MinuteFormat = {\n time: string,\n};\n\ntype DateForTemplate = {\n old: Date,\n recent: Date,\n};\n\ntype ParamForTemplateConfig = {\n templateConfig: DiffTemplate,\n dates: DateForTemplate,\n};\n\nconst formatMinuteString = (minute: number): MinuteFormat => {\n const str = minute.toString();\n\n return {\n time: str.length === 2 ? str : `0${str}`,\n };\n};\n\nconst formatHourString = (hour: number): HourFormat => {\n const period = hour < 12 ? 'am' : 'pm';\n const twelveHourTime = hour > 12 ? hour - 12 : hour;\n\n return {\n time: twelveHourTime === 0 ? '12' : twelveHourTime.toString(),\n period,\n };\n};\n\nconst getTemplatingParameters = (msDiff: number, config: ParamForTemplateConfig) => {\n const { dates, templateConfig } = config;\n const { timeLabel, monthLocaleLabels, timeTemplate, fullDateTemplate } = templateConfig;\n\n if (msDiff < MS_HOUR) {\n const minuteDiff = msToMinute(msDiff);\n const minuteUnit = minuteDiff > 1\n ? timeLabel.minute.plural\n : timeLabel.minute.singular;\n\n return {\n template: timeTemplate,\n params: {\n time: String(minuteDiff || 1),\n timeLabel: minuteUnit,\n },\n };\n }\n\n if (msDiff < MS_DAY) {\n const hourDiff = msToHour(msDiff);\n const hourUnit = hourDiff > 1\n ? timeLabel.hour.plural\n : timeLabel.hour.singular;\n\n return {\n template: timeTemplate,\n params: {\n time: String(hourDiff),\n timeLabel: hourUnit,\n },\n };\n }\n\n const monthLabel = monthLocaleLabels[dates.old.getMonth()];\n\n const minute = formatMinuteString(dates.old.getMinutes());\n const hour = formatHourString(dates.old.getHours());\n\n const dayFormat = `${monthLabel} ${dates.old.getDate()}`;\n const timeFormat = `${hour.time}:${minute.time} ${hour.period}`;\n\n return {\n template: fullDateTemplate,\n params: {\n date: `${dayFormat}, ${timeFormat}`,\n },\n };\n};\n\n/**\n * The returned string follows the following rules:\n * - If diff < 60 minutes: '29 min ago'\n * - If diff >= 60 minutes & < 24 hours: '12 hours ago' | '1 hour ago'\n * - If diff > 24 hours: 'on March 15, 14:45 pm'\n *\n * @param olderDate - Date\n * @param recentDate - Date\n * @param template - DiffTemplate\n * @returns a preformated string to be inserted in a phrase.\n */\nexport const getTimeDiffAsString = (olderDate: Date, recentDate = new Date(), templateConfig = defaultEnglishTemplate): string => {\n const oldDateTime = olderDate.getTime();\n const recentDateTime = recentDate.getTime();\n\n if (oldDateTime > recentDateTime) {\n return '';\n }\n\n const msDiff = recentDateTime - oldDateTime;\n const { template, params } = getTemplatingParameters(msDiff, {\n dates: {\n old: olderDate,\n recent: recentDate,\n },\n templateConfig,\n });\n\n return replaceInTemplate(template, params, TEMPLATE_CONFIG);\n};\n"],"names":["MS_HOUR","msToMinute","MS_DAY","msToHour","replaceInTemplate"],"mappings":";;;;;;;;AAyCA,MAAM,mBAAmB,GAAsB;IAC7C,SAAS;IACT,UAAU;IACV,OAAO;IACP,OAAO;IACP,KAAK;IACL,MAAM;IACN,MAAM;IACN,QAAQ;IACR,WAAW;IACX,SAAS;IACT,UAAU;IACV,UAAU;CACX,CAAC;AAEF,MAAM,sBAAsB,GAAiB;AAC3C,IAAA,YAAY,EAAE,wBAAwB;AACtC,IAAA,gBAAgB,EAAE,WAAW;AAC7B,IAAA,iBAAiB,EAAE,mBAAmB;AACtC,IAAA,SAAS,EAAE;AACT,QAAA,MAAM,EAAE;AACN,YAAA,QAAQ,EAAE,KAAK;AACf,YAAA,MAAM,EAAE,KAAK;AACd,SAAA;AACD,QAAA,IAAI,EAAE;AACJ,YAAA,QAAQ,EAAE,MAAM;AAChB,YAAA,MAAM,EAAE,OAAO;AAChB,SAAA;AACF,KAAA;CACF,CAAC;AAEF,MAAM,eAAe,GAAkB;AACrC,IAAA,SAAS,EAAE;AACT,QAAA,IAAI,EAAE,GAAG;AACT,QAAA,KAAK,EAAE,GAAG;AACX,KAAA;C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