@vope/utils 1.4.9 → 1.4.11

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/cjs/index.js CHANGED
@@ -24,6 +24,7 @@ __export(src_exports, {
24
24
  MemoryBasic: () => MemoryBasic,
25
25
  MemoryList: () => MemoryList,
26
26
  MemoryPower: () => MemoryPower,
27
+ Subscriber: () => Subscriber,
27
28
  Union: () => Union,
28
29
  clamp: () => clamp,
29
30
  classnames: () => classnames,
@@ -523,6 +524,40 @@ var css = (...args) => {
523
524
  return list.join(";");
524
525
  };
525
526
 
527
+ // src/subscriber.ts
528
+ var Subscriber = class {
529
+ #listeners = {};
530
+ $on(name, listener) {
531
+ const listeners = this.#listeners;
532
+ if (name in listeners) {
533
+ listeners[name].push(listener);
534
+ } else {
535
+ listeners[name] = [listener];
536
+ }
537
+ return this;
538
+ }
539
+ $emit(name, ...args) {
540
+ this.#listeners[name]?.forEach((listener) => listener(...args));
541
+ return this;
542
+ }
543
+ $off(name, listener) {
544
+ const listeners = this.#listeners;
545
+ if (name in listeners === false)
546
+ return this;
547
+ if (listener) {
548
+ listeners[name].some((value, index) => {
549
+ if (value === listener) {
550
+ listeners[name].splice(index, 1);
551
+ return true;
552
+ }
553
+ });
554
+ } else {
555
+ delete listeners[name];
556
+ }
557
+ return this;
558
+ }
559
+ };
560
+
526
561
  // src/table/flex.ts
527
562
  var FlexTable = class {
528
563
  #data = [];
@@ -631,6 +666,7 @@ var Union = class {
631
666
  }
632
667
  #data = {};
633
668
  #options = {};
669
+ #raws = {};
634
670
  get data() {
635
671
  return this.#data;
636
672
  }
@@ -640,10 +676,32 @@ var Union = class {
640
676
  add(data) {
641
677
  const key = data[this.key];
642
678
  const result = this.formatter(data);
679
+ this.#raws[key] = data;
643
680
  this.#data[key] = result.data;
644
681
  this.#options[key] = result.option;
645
682
  return this;
646
683
  }
684
+ /**
685
+ * 获取原始数据
686
+ */
687
+ getRaw(key) {
688
+ return this.#raws[key];
689
+ }
690
+ /**
691
+ * 从已有的联合中拷贝数据
692
+ */
693
+ clone(union, key) {
694
+ const { data, options } = union;
695
+ const keys = Array.isArray(key) ? key : [key];
696
+ keys.forEach((key2) => {
697
+ if (key2 in data) {
698
+ this.#raws[key2] = union.getRaw(key2);
699
+ this.#data[key2] = data[key2];
700
+ this.#options[key2] = options[key2];
701
+ }
702
+ });
703
+ return this;
704
+ }
647
705
  };
648
706
 
649
707
  // src/url/join.ts
package/lib/index.js CHANGED
@@ -21,6 +21,7 @@ import { deconPowersSum } from "./power/decon";
21
21
  import { rotateAngle } from "./rotate/angle";
22
22
  import { rotateRad } from "./rotate/rad";
23
23
  import { css } from "./style/css";
24
+ import { default as default2 } from "./subscriber";
24
25
  import { FlexTable } from "./table/flex";
25
26
  import { Union } from "./union";
26
27
  import { joinPaths } from "./url/join";
@@ -31,6 +32,7 @@ export {
31
32
  MemoryBasic,
32
33
  MemoryList,
33
34
  MemoryPower,
35
+ default2 as Subscriber,
34
36
  Union,
35
37
  clamp,
36
38
  classnames,
@@ -0,0 +1,35 @@
1
+ class Subscriber {
2
+ #listeners = {};
3
+ $on(name, listener) {
4
+ const listeners = this.#listeners;
5
+ if (name in listeners) {
6
+ listeners[name].push(listener);
7
+ } else {
8
+ listeners[name] = [listener];
9
+ }
10
+ return this;
11
+ }
12
+ $emit(name, ...args) {
13
+ this.#listeners[name]?.forEach((listener) => listener(...args));
14
+ return this;
15
+ }
16
+ $off(name, listener) {
17
+ const listeners = this.#listeners;
18
+ if (name in listeners === false)
19
+ return this;
20
+ if (listener) {
21
+ listeners[name].some((value, index) => {
22
+ if (value === listener) {
23
+ listeners[name].splice(index, 1);
24
+ return true;
25
+ }
26
+ });
27
+ } else {
28
+ delete listeners[name];
29
+ }
30
+ return this;
31
+ }
32
+ }
33
+ export {
34
+ Subscriber as default
35
+ };
package/lib/union.js CHANGED
@@ -5,6 +5,7 @@ class Union {
5
5
  }
6
6
  #data = {};
7
7
  #options = {};
8
+ #raws = {};
8
9
  get data() {
9
10
  return this.#data;
10
11
  }
@@ -14,10 +15,32 @@ class Union {
14
15
  add(data) {
15
16
  const key = data[this.key];
16
17
  const result = this.formatter(data);
18
+ this.#raws[key] = data;
17
19
  this.#data[key] = result.data;
18
20
  this.#options[key] = result.option;
19
21
  return this;
20
22
  }
23
+ /**
24
+ * 获取原始数据
25
+ */
26
+ getRaw(key) {
27
+ return this.#raws[key];
28
+ }
29
+ /**
30
+ * 从已有的联合中拷贝数据
31
+ */
32
+ clone(union, key) {
33
+ const { data, options } = union;
34
+ const keys = Array.isArray(key) ? key : [key];
35
+ keys.forEach((key2) => {
36
+ if (key2 in data) {
37
+ this.#raws[key2] = union.getRaw(key2);
38
+ this.#data[key2] = data[key2];
39
+ this.#options[key2] = options[key2];
40
+ }
41
+ });
42
+ return this;
43
+ }
21
44
  }
22
45
  export {
23
46
  Union
package/package.json CHANGED
@@ -1,57 +1,57 @@
1
- {
2
- "name": "@vope/utils",
3
- "version": "1.4.9",
4
- "description": "",
5
- "type": "module",
6
- "publishConfig": {
7
- "access": "public",
8
- "registry": "https://registry.npmjs.org/"
9
- },
10
- "types": "./types/index.d.ts",
11
- "typings": "./types/index.d.ts",
12
- "module": "./lib/index.js",
13
- "main": "./cjs/index.js",
14
- "scripts": {
15
- "start": "esbuild ./pre.ts --outfile=pre.cjs --format=cjs & node ./pre.cjs",
16
- "prebuild": "npm run start & esbuild ./build.ts --outfile=build.cjs --format=cjs",
17
- "build": "node ./build.cjs",
18
- "premocha": "node ./build.cjs --type=test",
19
- "mocha": "mocha __tests/**/*.js",
20
- "test": "npm run mocha",
21
- "prepublishOnly": "npm run build & npm run test"
22
- },
23
- "exports": {
24
- ".": {
25
- "import": "./lib/index.js",
26
- "require": "./cjs/index.js"
27
- }
28
- },
29
- "author": "vope",
30
- "license": "ISC",
31
- "files": [
32
- "cjs",
33
- "lib",
34
- "types"
35
- ],
36
- "devDependencies": {
37
- "@babel/parser": "^7.23.4",
38
- "@babel/traverse": "^7.23.4",
39
- "@babel/types": "^7.23.4",
40
- "@types/babel__traverse": "^7.20.4",
41
- "@types/mocha": "^10.0.6",
42
- "@types/node": "^20.4.5",
43
- "esbuild": "^0.19.4",
44
- "glob": "^10.3.10",
45
- "iconv-lite": "^0.6.3",
46
- "mocha": "^10.2.0",
47
- "rimraf": "^5.0.5",
48
- "tslib": "^2.6.1",
49
- "typescript": "^5.3.2"
50
- },
51
- "dependencies": {
52
- "@types/lodash-es": "^4.17.9",
53
- "@vope/types": "^1.0.4",
54
- "csstype": "^3.1.3",
55
- "lodash-es": "^4.17.21"
56
- }
57
- }
1
+ {
2
+ "name": "@vope/utils",
3
+ "version": "1.4.11",
4
+ "description": "",
5
+ "type": "module",
6
+ "publishConfig": {
7
+ "access": "public",
8
+ "registry": "https://registry.npmjs.org/"
9
+ },
10
+ "types": "./types/index.d.ts",
11
+ "typings": "./types/index.d.ts",
12
+ "module": "./lib/index.js",
13
+ "main": "./cjs/index.js",
14
+ "scripts": {
15
+ "start": "esbuild ./pre.ts --outfile=pre.cjs --format=cjs & node ./pre.cjs",
16
+ "prebuild": "npm run start & esbuild ./build.ts --outfile=build.cjs --format=cjs",
17
+ "build": "node ./build.cjs",
18
+ "premocha": "node ./build.cjs --type=test",
19
+ "mocha": "mocha __tests/**/*.js",
20
+ "test": "npm run mocha",
21
+ "prepublishOnly": "npm run build & npm run test"
22
+ },
23
+ "exports": {
24
+ ".": {
25
+ "import": "./lib/index.js",
26
+ "require": "./cjs/index.js"
27
+ }
28
+ },
29
+ "author": "vope",
30
+ "license": "ISC",
31
+ "files": [
32
+ "cjs",
33
+ "lib",
34
+ "types"
35
+ ],
36
+ "devDependencies": {
37
+ "@babel/parser": "^7.23.4",
38
+ "@babel/traverse": "^7.23.4",
39
+ "@babel/types": "^7.23.4",
40
+ "@types/babel__traverse": "^7.20.4",
41
+ "@types/mocha": "^10.0.6",
42
+ "@types/node": "^20.4.5",
43
+ "esbuild": "^0.19.4",
44
+ "glob": "^10.3.10",
45
+ "iconv-lite": "^0.6.3",
46
+ "mocha": "^10.2.0",
47
+ "rimraf": "^5.0.5",
48
+ "tslib": "^2.6.1",
49
+ "typescript": "^5.3.3"
50
+ },
51
+ "dependencies": {
52
+ "@types/lodash-es": "^4.17.9",
53
+ "@vope/types": "^1.0.12",
54
+ "csstype": "^3.1.3",
55
+ "lodash-es": "^4.17.21"
56
+ }
57
+ }
package/types/index.d.ts CHANGED
@@ -21,6 +21,7 @@ export { deconPowersSum } from './power/decon';
21
21
  export { rotateAngle } from './rotate/angle';
22
22
  export { rotateRad } from './rotate/rad';
23
23
  export { type CSSProperties, type StyleValue, css } from './style/css';
24
+ export { default as Subscriber } from './subscriber';
24
25
  export { FlexTable } from './table/flex';
25
26
  export { Union } from './union';
26
27
  export { joinPaths } from './url/join';
@@ -0,0 +1,9 @@
1
+ /**
2
+ * 订阅
3
+ */
4
+ export default class Subscriber<T extends Record<string, (...args: any[]) => void>> {
5
+ #private;
6
+ $on<K extends keyof T>(name: K, listener: T[K]): this;
7
+ $emit<K extends keyof T>(name: K, ...args: Parameters<T[K]>): this;
8
+ $off<K extends keyof T>(name: K, listener?: T[K]): this;
9
+ }
package/types/union.d.ts CHANGED
@@ -3,6 +3,12 @@ interface Result<Data, Option> {
3
3
  data: Data;
4
4
  option: Option;
5
5
  }
6
+ type Raws<Primary extends string, Status extends string, Origin extends {
7
+ [key in Primary]: string;
8
+ }> = {
9
+ [P in Status]: Revise<Origin, Primary, P>;
10
+ };
11
+ type GetStatus<T> = T extends Union<any, any, any, any, any, infer S> ? S : never;
6
12
  /**
7
13
  * 数据联合(用于需要对加入的数据进行智能提示)
8
14
  */
@@ -10,11 +16,19 @@ export declare class Union<Primary extends string, Member extends string, Origin
10
16
  [key in Primary]: string;
11
17
  }, Data extends unknown, Option extends unknown, Status extends Member | never = never> {
12
18
  #private;
13
- private key;
19
+ protected key: Primary;
14
20
  private formatter;
15
21
  get data(): Required<Record<Status, Data>>;
16
22
  get options(): Required<Record<Status, Option>>;
17
23
  constructor(key: Primary, formatter: (data: Origin) => Result<Data, Option>);
18
- add<V extends Exclude<Member, Status>>(data: Revise<Origin, Primary, V>): Union<Primary, Member, Origin, Data, Option, Status | V> & Omit<this, keyof Union<Primary, Member, Origin, Data, Option, Status | V>>;
24
+ add<P extends Exclude<Member, Status>>(data: Revise<Origin, Primary, P>): Union<Primary, Member, Origin, Data, Option, Status | P> & Omit<this, keyof Union<Primary, Member, Origin, Data, Option, Status | P>>;
25
+ /**
26
+ * 获取原始数据
27
+ */
28
+ getRaw<P extends Status>(key: P): Raws<Primary, Status, Origin>[P];
29
+ /**
30
+ * 从已有的联合中拷贝数据
31
+ */
32
+ clone<T extends Union<Primary, string, Origin, Data, Option>, P extends GetStatus<T> & Member>(union: T, key: P | P[]): Union<Primary, Member, Origin, Data, Option, Status | P> & Omit<this, keyof Union<Primary, Member, Origin, Data, Option, Status | P>>;
19
33
  }
20
34
  export {};