@neovici/cosmoz-utils 4.0.0-beta.2 → 5.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/dist/array.d.ts +1 -0
  2. package/dist/array.js +24 -0
  3. package/dist/date.d.ts +39 -0
  4. package/dist/date.js +147 -0
  5. package/dist/directives/lazy-until.d.ts +43 -0
  6. package/dist/directives/lazy-until.js +142 -0
  7. package/dist/directives/measure.d.ts +12 -0
  8. package/dist/directives/measure.js +24 -0
  9. package/dist/directives/portal.d.ts +15 -0
  10. package/dist/directives/portal.js +58 -0
  11. package/dist/directives/spread-props.d.ts +9 -0
  12. package/dist/directives/spread-props.js +22 -0
  13. package/dist/elements/cz-icon.d.ts +8 -0
  14. package/dist/elements/cz-icon.js +15 -0
  15. package/dist/elements/cz-spinner.d.ts +1 -0
  16. package/dist/elements/cz-spinner.js +22 -0
  17. package/dist/elements/index.d.ts +2 -0
  18. package/dist/elements/index.js +2 -0
  19. package/dist/function.d.ts +5 -0
  20. package/dist/function.js +4 -0
  21. package/dist/haunted-polymer.d.ts +341 -0
  22. package/dist/haunted-polymer.js +83 -0
  23. package/dist/hooks/use-debounce-raf.d.ts +16 -0
  24. package/dist/hooks/use-debounce-raf.js +36 -0
  25. package/dist/hooks/use-dropped-files.d.ts +1 -0
  26. package/dist/hooks/use-dropped-files.js +7 -0
  27. package/dist/hooks/use-handle-drop.d.ts +1 -0
  28. package/dist/hooks/use-handle-drop.js +18 -0
  29. package/dist/hooks/use-host-bounds.d.ts +2 -0
  30. package/dist/hooks/use-host-bounds.js +12 -0
  31. package/dist/hooks/use-host.d.ts +3 -0
  32. package/dist/hooks/use-host.js +6 -0
  33. package/dist/hooks/use-imperative-api.d.ts +1 -0
  34. package/dist/hooks/use-imperative-api.js +18 -0
  35. package/dist/hooks/use-meta.d.ts +8 -0
  36. package/{lib → dist}/hooks/use-meta.js +3 -5
  37. package/dist/hooks/use-notify-property.d.ts +2 -0
  38. package/dist/hooks/use-notify-property.js +19 -0
  39. package/dist/hooks/use-promise.d.ts +1 -0
  40. package/dist/hooks/use-promise.js +42 -0
  41. package/dist/index.d.ts +26 -0
  42. package/{index.js → dist/index.js} +14 -24
  43. package/dist/money.d.ts +47 -0
  44. package/dist/money.js +86 -0
  45. package/dist/object.d.ts +21 -0
  46. package/dist/object.js +56 -0
  47. package/dist/promise.d.ts +10 -0
  48. package/dist/promise.js +66 -0
  49. package/dist/reduce/action.d.ts +13 -0
  50. package/dist/reduce/action.js +10 -0
  51. package/{lib/reduce/index.js → dist/reduce/index.d.ts} +0 -0
  52. package/dist/reduce/index.js +2 -0
  53. package/dist/reduce/reduce.d.ts +1 -0
  54. package/dist/reduce/reduce.js +12 -0
  55. package/dist/tag.d.ts +23 -0
  56. package/dist/tag.js +28 -0
  57. package/dist/tagged.d.ts +1 -0
  58. package/{lib → dist}/tagged.js +0 -0
  59. package/dist/template.d.ts +47 -0
  60. package/dist/template.js +66 -0
  61. package/package.json +20 -6
  62. package/lib/array.js +0 -27
  63. package/lib/date.js +0 -176
  64. package/lib/directives/lazy-until.js +0 -163
  65. package/lib/directives/measure.js +0 -32
  66. package/lib/directives/portal.js +0 -65
  67. package/lib/directives/spread-props.js +0 -31
  68. package/lib/function.js +0 -21
  69. package/lib/haunted-polymer.js +0 -93
  70. package/lib/hooks/use-debounce-raf.js +0 -49
  71. package/lib/hooks/use-dropped-files.js +0 -12
  72. package/lib/hooks/use-handle-drop.js +0 -22
  73. package/lib/hooks/use-host-bounds.js +0 -21
  74. package/lib/hooks/use-host.js +0 -9
  75. package/lib/hooks/use-imperative-api.js +0 -22
  76. package/lib/hooks/use-notify-property.js +0 -26
  77. package/lib/hooks/use-promise.js +0 -74
  78. package/lib/money.js +0 -105
  79. package/lib/object.js +0 -26
  80. package/lib/promise.js +0 -88
  81. package/lib/reduce/action.js +0 -16
  82. package/lib/reduce/reduce.js +0 -16
  83. package/lib/tag.js +0 -34
  84. package/lib/template.js +0 -89
@@ -0,0 +1,42 @@
1
+ import { useReducer, useEffect } from 'haunted';
2
+ import { reduce, action } from '../reduce';
3
+ const states = {
4
+ pending: 'pending',
5
+ rejected: 'rejected',
6
+ resolved: 'resolved'
7
+ }, initial = {
8
+ error: undefined,
9
+ result: undefined,
10
+ state: states.pending
11
+ }, pending = action(states.pending), resolved = action(states.resolved, result => ({ result })), rejected = action(states.rejected, error => ({ error })), reducer = reduce(initial, [
12
+ [pending, () => ({
13
+ error: undefined,
14
+ result: undefined,
15
+ state: states.pending
16
+ })],
17
+ [resolved, (state, { result }) => ({
18
+ error: undefined,
19
+ result,
20
+ state: states.resolved
21
+ })],
22
+ [rejected, (state, { error }) => ({
23
+ error,
24
+ result: undefined,
25
+ state: states.rejected
26
+ })]
27
+ ]);
28
+ export const usePromise = promise => {
29
+ const [{ error, result, state }, dispatch] = useReducer(reducer, initial);
30
+ useEffect(() => {
31
+ if (!promise) {
32
+ return;
33
+ }
34
+ let canceled = false;
35
+ dispatch(pending());
36
+ promise.then(result => !canceled && dispatch(resolved(result)), error => !canceled && dispatch(rejected(error)));
37
+ return () => {
38
+ canceled = true;
39
+ };
40
+ }, [promise]);
41
+ return [result, error, state];
42
+ };
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Creates a mixin with the specified functions applied to the superclass
3
+ *
4
+ * ```js
5
+ * import { mixin, Money } from '@neovici/cosmoz-utils';
6
+ * import { isEmpty } from '@neovici/cosmoz-utils/template';
7
+ * import { isoDate } from '@neovici/cosmoz-utils/date';
8
+ *
9
+ * class DemoMoneyHelper extends mixin({isEmpty, isoDate, ...Money}, PolymerElement) {
10
+ * ```
11
+ *
12
+ * @mixinFunction
13
+ * @demo demo/index.html
14
+ * @param {object} helpers the functions to add to the class
15
+ * @param {class} superclass the class to extend
16
+ * @return {class} a new class
17
+ */
18
+ import { hauntedPolymer, Constructor } from './haunted-polymer';
19
+ declare const mixin: <H, S extends object>(helpers: H, superclass: Constructor<S>) => {
20
+ new (...args: any[]): {};
21
+ };
22
+ import * as Template from './template';
23
+ import * as DateUtils from './date';
24
+ import * as Money from './money';
25
+ export { hauntedPolymer, Template, DateUtils, mixin, Money, DateUtils as Date };
26
+ export * from './tagged';
@@ -15,30 +15,20 @@
15
15
  * @param {class} superclass the class to extend
16
16
  * @return {class} a new class
17
17
  */
18
+ import { hauntedPolymer } from './haunted-polymer';
18
19
  const mixin = (helpers, superclass) => {
19
- /**
20
- * @polymer
21
- * @mixinClass
22
- */
23
- const MixedElement = class extends superclass {};
24
- Object.assign(MixedElement.prototype, helpers);
25
- return MixedElement;
20
+ /**
21
+ * @polymer
22
+ * @mixinClass
23
+ */
24
+ const MixedElement = class extends superclass {
25
+ };
26
+ Object.assign(MixedElement.prototype, helpers);
27
+ return MixedElement;
26
28
  };
27
-
28
- import * as Template from './lib/template';
29
- import * as DateUtils from './lib/date';
30
- import * as Money from './lib/money';
31
-
32
- import { hauntedPolymer } from './lib/haunted-polymer';
33
-
29
+ import * as Template from './template';
30
+ import * as DateUtils from './date';
31
+ import * as Money from './money';
34
32
  // TODO remove deprecated Date export [issue #34]
35
- export {
36
- hauntedPolymer,
37
- Template,
38
- DateUtils,
39
- mixin,
40
- Money,
41
- DateUtils as Date
42
- };
43
-
44
- export * from './lib/tagged';
33
+ export { hauntedPolymer, Template, DateUtils, mixin, Money, DateUtils as Date };
34
+ export * from './tagged';
@@ -0,0 +1,47 @@
1
+ export interface Amount {
2
+ amount: number;
3
+ currency: string;
4
+ }
5
+ declare const
6
+ /**
7
+ * Determine if a constant or a variable is an amount.
8
+ * @param {object} potentialAmount Potential amount.
9
+ * @returns {boolean} Whether the potential amount is a valid amount object with amount and currency.
10
+ */
11
+ isAmount: (potentialAmount: unknown) => potentialAmount is Amount,
12
+ /**
13
+ * Render an amount with decimal separator and currency symbol.
14
+ * @param {object} money Money with amount property and optionally currency property.
15
+ * @param {void|string} locale Locale to format the amount in.
16
+ * @return {string} Formatted amount.
17
+ */
18
+ renderAmount: (money: Amount | null, locale?: string) => string | undefined,
19
+ /**
20
+ * Alias for renderAmount(money). Render an amount with decimal separator and currency symbol.
21
+ * @param {object} money Money with amount property and optionally currency property.
22
+ * @return {string} Formatted amount.
23
+ */
24
+ renderMoney: (money: Amount | null, locale?: string) => string | undefined,
25
+ /**
26
+ * Render an amount with decimal separator but without currency symbol.
27
+ * @param {object} money Money with amount property and optionally currency property.
28
+ * @param {void|string} locale Locale to format the amount in.
29
+ * @return {string} Formatted number.
30
+ */
31
+ renderNumberAmount: (money: Amount, locale?: string) => string,
32
+ /**
33
+ * Round a number to a given precision.
34
+ * @param {string} number Number with decimals to round.
35
+ * @param {number} precision Number of decimals to round amount to.
36
+ * @return {number} Rounded number.
37
+ */
38
+ round: (number: number | string, precision: number) => number,
39
+ /**
40
+ * Compare amounts.
41
+ * @param {object} amount1 Amount 1.
42
+ * @param {object} amount2 Amount 2.
43
+ * @param {number} precision Decimal precision, defaults to 2 decimals.
44
+ * @returns {boolean} Whether the amounts are equal regarding amount to the specified precision and the currency.
45
+ */
46
+ amountEquals: <T1, T2>(amount1: T1, amount2: T2, precision?: number) => boolean;
47
+ export { isAmount, renderAmount, renderMoney, renderNumberAmount, round, amountEquals, };
package/dist/money.js ADDED
@@ -0,0 +1,86 @@
1
+ const CURRENCY_FORMATTERS = {}, NUMBER_FORMATTERS = {}, _getCurrencyFormatter = function (currency, locale, currencyDisplay = 'code') {
2
+ if (currency == null) {
3
+ return;
4
+ }
5
+ const key = currency.toUpperCase() + (locale || '');
6
+ if (CURRENCY_FORMATTERS[key] == null) {
7
+ CURRENCY_FORMATTERS[key] = new Intl.NumberFormat(locale, {
8
+ style: 'currency',
9
+ currency,
10
+ currencyDisplay,
11
+ });
12
+ }
13
+ return CURRENCY_FORMATTERS[key];
14
+ },
15
+ /**
16
+ * Determine if a constant or a variable is an amount.
17
+ * @param {object} potentialAmount Potential amount.
18
+ * @returns {boolean} Whether the potential amount is a valid amount object with amount and currency.
19
+ */
20
+ isAmount = (potentialAmount) => potentialAmount != null &&
21
+ typeof potentialAmount === 'object' &&
22
+ typeof potentialAmount.amount === 'number' &&
23
+ typeof potentialAmount.currency === 'string' &&
24
+ potentialAmount.currency.length === 3,
25
+ /**
26
+ * Render an amount with decimal separator and currency symbol.
27
+ * @param {object} money Money with amount property and optionally currency property.
28
+ * @param {void|string} locale Locale to format the amount in.
29
+ * @return {string} Formatted amount.
30
+ */
31
+ renderAmount = (money, locale) => {
32
+ if (money?.amount == null) {
33
+ return;
34
+ }
35
+ const formatter = _getCurrencyFormatter(money.currency, locale);
36
+ if (formatter == null) {
37
+ return;
38
+ }
39
+ return formatter.format(money.amount);
40
+ },
41
+ /**
42
+ * Alias for renderAmount(money). Render an amount with decimal separator and currency symbol.
43
+ * @param {object} money Money with amount property and optionally currency property.
44
+ * @return {string} Formatted amount.
45
+ */
46
+ renderMoney = renderAmount,
47
+ /**
48
+ * Render an amount with decimal separator but without currency symbol.
49
+ * @param {object} money Money with amount property and optionally currency property.
50
+ * @param {void|string} locale Locale to format the amount in.
51
+ * @return {string} Formatted number.
52
+ */
53
+ renderNumberAmount = (money, locale) => {
54
+ const key = locale || '0';
55
+ if (NUMBER_FORMATTERS[key] == null) {
56
+ NUMBER_FORMATTERS[key] = new Intl.NumberFormat(locale, {
57
+ minimumFractionDigits: 2,
58
+ maximumFractionDigits: 2,
59
+ });
60
+ }
61
+ return NUMBER_FORMATTERS[key].format(money.amount);
62
+ },
63
+ /**
64
+ * Round a number to a given precision.
65
+ * @param {string} number Number with decimals to round.
66
+ * @param {number} precision Number of decimals to round amount to.
67
+ * @return {number} Rounded number.
68
+ */
69
+ round = (number, precision) => {
70
+ const fixed = Number(Math.round(Number(number + 'e' + precision)) + 'e-' + precision).toFixed(precision);
71
+ return parseFloat(fixed);
72
+ },
73
+ /**
74
+ * Compare amounts.
75
+ * @param {object} amount1 Amount 1.
76
+ * @param {object} amount2 Amount 2.
77
+ * @param {number} precision Decimal precision, defaults to 2 decimals.
78
+ * @returns {boolean} Whether the amounts are equal regarding amount to the specified precision and the currency.
79
+ */
80
+ amountEquals = (amount1, amount2, precision = 2) => {
81
+ return (isAmount(amount1) &&
82
+ isAmount(amount2) &&
83
+ round(amount1.amount, precision) === round(amount2.amount, precision) &&
84
+ amount1.currency === amount2.currency);
85
+ };
86
+ export { isAmount, renderAmount, renderMoney, renderNumberAmount, round, amountEquals, };
@@ -0,0 +1,21 @@
1
+ import { identity } from './function';
2
+ declare type Rec<K extends PropertyKey = PropertyKey, V = any> = Record<K, V>;
3
+ export declare function prop(): typeof identity;
4
+ export declare function prop(a: '' | null | false | undefined | 0): typeof identity;
5
+ export declare function prop<K extends PropertyKey>(key: K): <O extends Rec<K>>(obj: O) => O[K];
6
+ export declare const strProp: <K extends PropertyKey>(key: K) => <O extends Rec<K, any>>(o: O) => string;
7
+ export declare const transform: <K extends string, V, RK extends PropertyKey, RV>(obj: Record<K, V>, trans: (entries: [Extract<K, string>, V][]) => Iterable<readonly [RK, RV]>) => { [key in RK]: RV; };
8
+ export declare const omit: <K extends PropertyKey>(keys: K[]) => <T extends Rec<PropertyKey, any>>(obj: T) => { [key in Exclude<keyof T, K>]: T[key]; };
9
+ export declare const props: <K extends PropertyKey>(keys: K[]) => <T extends Rec<PropertyKey, any>>(obj: T) => { [key in K & keyof T]: T[key]; };
10
+ declare type Merge<T1, T2> = {
11
+ [key in Exclude<keyof T1, keyof T2>]: T1[key];
12
+ } & {
13
+ [key in Exclude<keyof T2, keyof T1>]: T2[key];
14
+ } & {
15
+ [key in keyof T1 & keyof T2]: T1 extends Rec ? T2 extends Rec ? Merge<T1[key], T2[key]> : T2[key] : T2[key];
16
+ };
17
+ export declare const isObject: (obj: unknown) => obj is object;
18
+ export declare function merge<T1, T2, T3>(a1: T1, a2: T2, a3: T3): Merge<Merge<T1, T2>, T3>;
19
+ export declare function merge<T1, T2>(a1: T1, b2: T2): Merge<T1, T2>;
20
+ export declare function merge(...objs: Rec[]): Rec;
21
+ export {};
package/dist/object.js ADDED
@@ -0,0 +1,56 @@
1
+ /* eslint-disable func-style, no-use-before-define, import/group-exports */
2
+ import { identity } from './function';
3
+ export function prop(key) {
4
+ if (!key) {
5
+ return identity;
6
+ }
7
+ return (obj) => obj[key];
8
+ }
9
+ export const strProp = (key) => {
10
+ const p = prop(key);
11
+ return (o) => {
12
+ if (typeof o === 'string') {
13
+ return o;
14
+ }
15
+ return p(o)?.toString() || '';
16
+ };
17
+ };
18
+ export const transform = (obj, trans) => Object.fromEntries(trans(Object.entries(obj)));
19
+ export const omit = (keys) => (obj) => {
20
+ const ret = {};
21
+ for (const key in obj) {
22
+ if (!keys.includes(key)) {
23
+ ret[key] = obj[key];
24
+ }
25
+ }
26
+ return ret;
27
+ };
28
+ export const props = (keys) => (obj) => {
29
+ const ret = {};
30
+ for (const key in obj) {
31
+ if (keys.includes(key)) {
32
+ ret[key] = obj[key];
33
+ }
34
+ }
35
+ return ret;
36
+ };
37
+ export const isObject = (obj) => obj != null && typeof obj === 'object' && obj.constructor === Object;
38
+ export function merge(...objs) {
39
+ return objs.reduce((acc, obj) => {
40
+ if (obj == null) {
41
+ return acc;
42
+ }
43
+ for (const key of Object.keys(obj)) {
44
+ if (isObject(obj[key]) && isObject(acc[key])) {
45
+ Object.assign(acc[key], merge(acc[key], obj[key]));
46
+ }
47
+ else if (Array.isArray(acc[key]) && Array.isArray(obj[key])) {
48
+ acc[key] = acc[key].concat(obj[key]);
49
+ }
50
+ else {
51
+ acc[key] = obj[key];
52
+ }
53
+ }
54
+ return acc;
55
+ }, {});
56
+ }
@@ -0,0 +1,10 @@
1
+ declare type Pfn<T> = (arg: T) => void;
2
+ export declare class ManagedPromise<T> extends Promise<T> {
3
+ constructor(callback?: (resolve: Pfn<T>, reject: Pfn<T>) => void);
4
+ }
5
+ export declare const timeout$: (ms?: number) => Promise<unknown>;
6
+ declare type Predicate<T extends Event> = (e: T) => boolean;
7
+ export declare const event$: <E extends Event, P extends Predicate<E>>(target: EventTarget, type: string, predicate?: P | undefined, timeout?: number) => Promise<unknown>;
8
+ export declare const limit$: <T extends [], P>(fn: (...args: T) => PromiseLike<P>, limit: number) => (...args: T) => Promise<P>;
9
+ export declare const debounce$: <T extends [], P>(fn: (...args: T) => PromiseLike<P>, ms?: number) => (...args: T) => Promise<unknown>;
10
+ export {};
@@ -0,0 +1,66 @@
1
+ /* eslint-disable import/group-exports */
2
+ export class ManagedPromise extends Promise {
3
+ constructor(callback) {
4
+ const handles = {};
5
+ super((resolve, reject) => Object.assign(handles, { resolve, reject }));
6
+ Object.assign(this, handles);
7
+ callback?.(handles.resolve, handles.reject);
8
+ }
9
+ }
10
+ export const timeout$ = (ms) => new Promise((res) => setTimeout(res, ms));
11
+ export const event$ = (target, type, predicate, timeout = 300000) => new Promise((resolve, reject) => {
12
+ let handler;
13
+ const tid = setTimeout(() => {
14
+ target.removeEventListener(type, handler);
15
+ reject(new Error('Timeout out'));
16
+ }, timeout);
17
+ target.addEventListener(type, (handler = (e) => {
18
+ if (predicate == null || predicate(e)) {
19
+ target.removeEventListener(type, handler);
20
+ clearTimeout(tid);
21
+ resolve(e);
22
+ }
23
+ }));
24
+ });
25
+ export const limit$ = (fn, limit) => {
26
+ const state = {
27
+ queue: [],
28
+ pending: [],
29
+ }, process = () => {
30
+ if (state.queue.length === 0) {
31
+ return;
32
+ }
33
+ if (state.pending.length >= limit) {
34
+ return;
35
+ }
36
+ const task = state.queue.shift();
37
+ state.pending.push(task);
38
+ Promise.resolve(fn(...task.args))
39
+ .then(task.resolve, task.reject)
40
+ .then(() => {
41
+ state.pending.splice(state.pending.indexOf(task), 1);
42
+ process();
43
+ });
44
+ };
45
+ return (...args) => new Promise((resolve, reject) => {
46
+ state.queue.push({ args, resolve, reject });
47
+ process();
48
+ });
49
+ };
50
+ export const debounce$ = (fn, ms) => {
51
+ let timeoutId;
52
+ const pending = [];
53
+ return (...args) => new Promise((res, rej) => {
54
+ clearTimeout(timeoutId);
55
+ timeoutId = setTimeout(() => {
56
+ const currentPending = [...pending];
57
+ pending.length = 0;
58
+ Promise.resolve(fn(...args)).then((data) => {
59
+ currentPending.forEach(({ resolve }) => resolve(data));
60
+ }, (error) => {
61
+ currentPending.forEach(({ reject }) => reject(error));
62
+ });
63
+ }, ms);
64
+ pending.push({ resolve: res, reject: rej });
65
+ });
66
+ };
@@ -0,0 +1,13 @@
1
+ declare type Creator<T extends [], R extends object> = (...args: T) => R;
2
+ export interface Action<T = unknown> {
3
+ type?: T;
4
+ toString: () => string;
5
+ }
6
+ declare const action: <T, A extends [], R extends object>(type: T, create?: Creator<A, R>) => ((...args: A) => R & {
7
+ type: T;
8
+ toString(): T;
9
+ }) & {
10
+ type: T;
11
+ toString(): T;
12
+ }, type: <T>(action: Action<T>) => string | T;
13
+ export { action, type };
@@ -0,0 +1,10 @@
1
+ const action = (type, create = () => ({})) => {
2
+ const common = {
3
+ type,
4
+ toString() {
5
+ return type;
6
+ },
7
+ }, callable = (...args) => Object.assign(create(...args), common);
8
+ return Object.assign(callable, common);
9
+ }, type = (action) => action.type || action.toString();
10
+ export { action, type };
File without changes
@@ -0,0 +1,2 @@
1
+ export * from './action';
2
+ export * from './reduce';
@@ -0,0 +1 @@
1
+ export function reduce(initial: any, handle: any): (state: any, action: any) => any;
@@ -0,0 +1,12 @@
1
+ import { type } from './action';
2
+ const ensureArray = x => Array.isArray(x) ? x : [x], reduce = (initial, handle) => {
3
+ const handles = ensureArray(handle), handlers = (handles.every(Array.isArray) ? handles : [handles]).map(([actions, handle]) => ({
4
+ actions: ensureArray(actions).map(type),
5
+ handle
6
+ }));
7
+ return (state = initial, action) => {
8
+ const handler = handlers.find(h => h.actions.includes(type(action)));
9
+ return handler ? handler.handle(state, action) : state;
10
+ };
11
+ };
12
+ export { reduce };
package/dist/tag.d.ts ADDED
@@ -0,0 +1,23 @@
1
+ export declare const
2
+ /**
3
+ * Dynamic component support for lit-html.
4
+ *
5
+ * Normally lit-html does not support interpolating the component tag, but we can trick it
6
+ * by calling the template function manually.
7
+ *
8
+ * NOTE: You can only use this function for interpolating a single tag and it must be the
9
+ * first interpolated value. If you need multiple components, you can do multiple interpolations.
10
+ *
11
+ * @example
12
+ * tag`<${name} some=${param}/>`
13
+ *
14
+ * @example
15
+ * html`A: ${ tag`<${ aTag }/>` } - B: ${ tag`${ bTag }` }`
16
+ *
17
+ * @param {string[]} strings The static strings.
18
+ * @param {string} component The interpolated component name.
19
+ * @param {any[]} values The interpolated values.
20
+ *
21
+ * @return {TemplateResult} The lit template result.
22
+ */
23
+ tag: (strings: string[], component: string, ...values: unknown[]) => import("lit-html").TemplateResult<1>;
package/dist/tag.js ADDED
@@ -0,0 +1,28 @@
1
+ import { html as htm } from 'haunted';
2
+ const html = (arr, ...thru) => htm(Object.assign(arr, { raw: true }), ...thru);
3
+ export const
4
+ /**
5
+ * Dynamic component support for lit-html.
6
+ *
7
+ * Normally lit-html does not support interpolating the component tag, but we can trick it
8
+ * by calling the template function manually.
9
+ *
10
+ * NOTE: You can only use this function for interpolating a single tag and it must be the
11
+ * first interpolated value. If you need multiple components, you can do multiple interpolations.
12
+ *
13
+ * @example
14
+ * tag`<${name} some=${param}/>`
15
+ *
16
+ * @example
17
+ * html`A: ${ tag`<${ aTag }/>` } - B: ${ tag`${ bTag }` }`
18
+ *
19
+ * @param {string[]} strings The static strings.
20
+ * @param {string} component The interpolated component name.
21
+ * @param {any[]} values The interpolated values.
22
+ *
23
+ * @return {TemplateResult} The lit template result.
24
+ */
25
+ tag = (strings, component, ...values) => html([
26
+ strings[0] + component + strings[1],
27
+ ...strings.slice(2)
28
+ ], ...values);
@@ -0,0 +1 @@
1
+ export declare const tagged: (strings: string[], ...values: string[]) => string;
File without changes
@@ -0,0 +1,47 @@
1
+ export declare const abs: (x: number) => number,
2
+ /**
3
+ * Check if any of the arguments are true.
4
+ * @return {Boolean} Whether any of the function arguments are true or not.
5
+ */
6
+ anyTrue: <T>(...args: T[]) => boolean,
7
+ /**
8
+ * Concatenate all arguments to a string.
9
+ * @return {string} Concatenated arguments.
10
+ */
11
+ concat: <T>(...args: T[]) => string,
12
+ /**
13
+ * If iftrue argument is true, return result argument, otherwise return elseresult argument.
14
+ * @param {boolean} iftrue Codition for result
15
+ * @param {*} result Result when iftrue is true.
16
+ * @param {*} elseresult Result when iftrue is false.
17
+ * @return {boolean} result or elseresult depending on iftrue evaluation.
18
+ */
19
+ ifElse: <T extends boolean, R, E>(iftrue: T, result: R, elseresult: E) => R | E,
20
+ /**
21
+ * Check if item argument exists in array argument.
22
+ * @param {*} item Item to search for in the array.
23
+ * @param {array} array Array to search.
24
+ * @return {boolean} Whether the item was found in array or not.
25
+ */
26
+ inArray: <T>(item: T, array: T[]) => boolean,
27
+ /**
28
+ * Check equality of the arguments.
29
+ * @param {*} arg1 First argument to compare.
30
+ * @param {*} arg2 Second argument to compare
31
+ * @return {boolean} Whether the first and second arguments are equal or not.
32
+ */
33
+ isEqual: <T>(arg1: T, arg2: T) => boolean,
34
+ /**
35
+ * Check if argument is undefined, null, empty Array list,
36
+ * empty String or 0 number (like length).
37
+ * @param {object} obj Argument to evaluate.
38
+ * @return {boolean} Whether the argument is empty or not.
39
+ */
40
+ isEmpty: <T>(obj: T) => boolean,
41
+ /**
42
+ * Formats a number using fixed-point notation.
43
+ * @param {number} number Number with decimals to be formatted.
44
+ * @param {number} fixval Number of decimals to use.
45
+ * @return {String} The number converted to string keep only fixval number of decimals, if number empty, returns empty string
46
+ */
47
+ toFixed: <T>(number: T, fixval: number) => string;
@@ -0,0 +1,66 @@
1
+ export const abs = Math.abs,
2
+ /**
3
+ * Check if any of the arguments are true.
4
+ * @return {Boolean} Whether any of the function arguments are true or not.
5
+ */
6
+ anyTrue = (...args) => args.some(arg => !!arg),
7
+ /**
8
+ * Concatenate all arguments to a string.
9
+ * @return {string} Concatenated arguments.
10
+ */
11
+ concat = (...args) => args.join(''),
12
+ /**
13
+ * If iftrue argument is true, return result argument, otherwise return elseresult argument.
14
+ * @param {boolean} iftrue Codition for result
15
+ * @param {*} result Result when iftrue is true.
16
+ * @param {*} elseresult Result when iftrue is false.
17
+ * @return {boolean} result or elseresult depending on iftrue evaluation.
18
+ */
19
+ ifElse = (iftrue, result, elseresult) => iftrue ? result : elseresult,
20
+ /**
21
+ * Check if item argument exists in array argument.
22
+ * @param {*} item Item to search for in the array.
23
+ * @param {array} array Array to search.
24
+ * @return {boolean} Whether the item was found in array or not.
25
+ */
26
+ inArray = (item, array) => array.indexOf(item) > -1,
27
+ /**
28
+ * Check equality of the arguments.
29
+ * @param {*} arg1 First argument to compare.
30
+ * @param {*} arg2 Second argument to compare
31
+ * @return {boolean} Whether the first and second arguments are equal or not.
32
+ */
33
+ isEqual = (arg1, arg2) => arg1 === arg2,
34
+ /**
35
+ * Check if argument is undefined, null, empty Array list,
36
+ * empty String or 0 number (like length).
37
+ * @param {object} obj Argument to evaluate.
38
+ * @return {boolean} Whether the argument is empty or not.
39
+ */
40
+ isEmpty = (obj) => {
41
+ if (obj === undefined || obj === null) {
42
+ return true;
43
+ }
44
+ if (Array.isArray(obj) && obj.length === 0) {
45
+ return true;
46
+ }
47
+ if (typeof obj === 'string' && obj.length === 0) {
48
+ return true;
49
+ }
50
+ if (typeof obj === 'number' && obj === 0) {
51
+ return true;
52
+ }
53
+ return false;
54
+ },
55
+ /**
56
+ * Formats a number using fixed-point notation.
57
+ * @param {number} number Number with decimals to be formatted.
58
+ * @param {number} fixval Number of decimals to use.
59
+ * @return {String} The number converted to string keep only fixval number of decimals, if number empty, returns empty string
60
+ */
61
+ toFixed = (number, fixval) => {
62
+ if (typeof number !== 'number') {
63
+ return '';
64
+ }
65
+ return number.toFixed(fixval);
66
+ };