@neovici/cosmoz-utils 5.29.0 → 5.31.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/array.d.ts CHANGED
@@ -5,3 +5,4 @@ export declare function array<T>(arr: Iterable<T>): T[];
5
5
  export declare function array<T = unknown>(arr: T): T[];
6
6
  export declare const without: <E, L = E>(exclude: E | E[], predicate?: <T extends E | L>(value: T) => unknown) => <T_1 extends L = L>(list: T_1 | T_1[]) => T_1[];
7
7
  export declare const chunk: <T>(list: T[], size: number) => T[][];
8
+ export declare const intersect: <T>(list: T[][]) => T[];
package/dist/array.js CHANGED
@@ -22,3 +22,7 @@ export const without = (exclude, predicate = identity) => (list) => {
22
22
  return array(list).filter((value) => !excludes.includes(predicate(value)));
23
23
  };
24
24
  export const chunk = (list, size) => [...Array(Math.ceil(list.length / size)).keys()].map((i) => list.slice(i * size, (i + 1) * size));
25
+ export const intersect = (list) => {
26
+ const [first = [], ...rest] = list.sort();
27
+ return first.filter((e) => rest.every((arr) => arr.includes(e)));
28
+ };
@@ -0,0 +1,18 @@
1
+ import { Context } from 'haunted/lib/create-context';
2
+ import { AttributePart } from 'lit-html';
3
+ import { AsyncDirective } from 'lit-html/async-directive.js';
4
+ import { ChildPart, DirectiveParameters } from 'lit-html/directive.js';
5
+ declare class ConsumeDirective<T> extends AsyncDirective {
6
+ value: T;
7
+ context: Context<T>;
8
+ pluck: (value: T) => unknown;
9
+ unsubscribe: VoidFunction | null;
10
+ raf: number;
11
+ update(part: AttributePart | ChildPart, [context, pluck]: DirectiveParameters<this>): unknown;
12
+ subscribe(emitter: Node): void;
13
+ render(context: Context<T>, pluck: (value: T) => unknown): unknown;
14
+ updater(value: T): void;
15
+ protected disconnected(): void;
16
+ }
17
+ export declare const consume: (context: Context<unknown>, pluck: (value: unknown) => unknown) => import("lit-html/directive.js").DirectiveResult<typeof ConsumeDirective>;
18
+ export {};
@@ -0,0 +1,54 @@
1
+ import { contextEvent } from 'haunted/lib/symbols';
2
+ import { noChange } from 'lit-html';
3
+ import { AsyncDirective, directive } from 'lit-html/async-directive.js';
4
+ import { identity } from '../function';
5
+ const getEmitter = (part) => 'element' in part ? part.element : part.parentNode;
6
+ class ConsumeDirective extends AsyncDirective {
7
+ value;
8
+ context;
9
+ pluck;
10
+ unsubscribe;
11
+ raf;
12
+ update(part, [context, pluck = identity]) {
13
+ // if the context has changed OR we are not yet subscribed
14
+ if (this.context !== context || !this.unsubscribe) {
15
+ this.unsubscribe?.();
16
+ this.context = context;
17
+ this.pluck = pluck;
18
+ // when first initialized, the element is not part of the DOM
19
+ // so we attempt to subscribe in the next animation frame
20
+ cancelAnimationFrame(this.raf);
21
+ this.raf = requestAnimationFrame(() => this.subscribe(getEmitter(part)));
22
+ return noChange;
23
+ }
24
+ if (this.pluck === pluck) {
25
+ return noChange;
26
+ }
27
+ this.pluck = pluck;
28
+ return this.render(context, pluck);
29
+ }
30
+ subscribe(emitter) {
31
+ const detail = { Context: this.context, callback: this.updater.bind(this) };
32
+ emitter.dispatchEvent(new CustomEvent(contextEvent, {
33
+ detail,
34
+ bubbles: true,
35
+ cancelable: true,
36
+ composed: true,
37
+ }));
38
+ const { unsubscribe = null, value } = detail;
39
+ this.value = unsubscribe ? value : this.context.defaultValue;
40
+ this.unsubscribe = unsubscribe;
41
+ this.setValue(this.pluck(this.value));
42
+ }
43
+ render(context, pluck) {
44
+ return pluck(this.value);
45
+ }
46
+ updater(value) {
47
+ this.value = value;
48
+ this.setValue(this.pluck(this.value));
49
+ }
50
+ disconnected() {
51
+ this.unsubscribe?.();
52
+ }
53
+ }
54
+ export const consume = directive(ConsumeDirective);
@@ -0,0 +1,16 @@
1
+ import { Context, ContextDetail } from 'haunted/lib/create-context';
2
+ import { AsyncDirective } from 'lit-html/async-directive.js';
3
+ import { ChildPart } from 'lit-html/directive.js';
4
+ declare class ProvideDirective<T> extends AsyncDirective {
5
+ value: T;
6
+ context: Context<T>;
7
+ listeners: Set<(value: T) => void>;
8
+ cleanup: VoidFunction | undefined;
9
+ update(part: ChildPart, [context, value]: [Context<T>, T]): symbol;
10
+ render(): symbol;
11
+ handleEvent(event: CustomEvent<ContextDetail<T>>): void;
12
+ unsubscribe(callback: (value: T) => void): void;
13
+ protected disconnected(): void;
14
+ }
15
+ export declare const provide: () => import("lit-html/directive.js").DirectiveResult<typeof ProvideDirective>;
16
+ export {};
@@ -0,0 +1,43 @@
1
+ import { contextEvent } from 'haunted/lib/symbols';
2
+ import { noChange, nothing } from 'lit-html';
3
+ import { AsyncDirective, directive } from 'lit-html/async-directive.js';
4
+ class ProvideDirective extends AsyncDirective {
5
+ value;
6
+ context;
7
+ listeners = new Set();
8
+ cleanup;
9
+ update(part, [context, value]) {
10
+ this.context = context;
11
+ if (this.value !== value) {
12
+ this.value = value;
13
+ for (const callback of this.listeners) {
14
+ callback(value);
15
+ }
16
+ }
17
+ if (!this.cleanup) {
18
+ part.parentNode.addEventListener(contextEvent, this);
19
+ this.cleanup = () => part.parentNode.removeEventListener(contextEvent, this);
20
+ }
21
+ return noChange;
22
+ }
23
+ render() {
24
+ return nothing;
25
+ }
26
+ handleEvent(event) {
27
+ const { detail } = event;
28
+ if (detail.Context !== this.context)
29
+ return;
30
+ detail.value = this.value;
31
+ detail.unsubscribe = this.unsubscribe.bind(this, detail.callback);
32
+ this.listeners.add(detail.callback);
33
+ event.stopPropagation();
34
+ }
35
+ unsubscribe(callback) {
36
+ this.listeners.delete(callback);
37
+ }
38
+ disconnected() {
39
+ this.cleanup?.();
40
+ this.cleanup = undefined;
41
+ }
42
+ }
43
+ export const provide = directive(ProvideDirective);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neovici/cosmoz-utils",
3
- "version": "5.29.0",
3
+ "version": "5.31.0",
4
4
  "description": "Date, money and template management functions commonly needed in Cosmoz views.",
5
5
  "keywords": [
6
6
  "polymer",