utilium 0.8.2 → 0.8.4

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/dom.d.ts CHANGED
@@ -7,3 +7,7 @@ export declare function upload(type?: string, multiple?: boolean): Promise<File>
7
7
  * Downloads some data
8
8
  */
9
9
  export declare function download(data: BlobPart, name: string): void;
10
+ /**
11
+ * Create an instance of a `<template>`
12
+ */
13
+ export declare function cloneTemplate(selector: string): DocumentFragment;
package/dist/dom.js CHANGED
@@ -27,3 +27,13 @@ export function download(data, name) {
27
27
  link.download = name;
28
28
  link.click();
29
29
  }
30
+ /**
31
+ * Create an instance of a `<template>`
32
+ */
33
+ export function cloneTemplate(selector) {
34
+ const template = document.querySelector(selector);
35
+ if (!template) {
36
+ throw new ReferenceError('Template does not exist: ' + selector);
37
+ }
38
+ return template.content.cloneNode(true);
39
+ }
package/dist/numbers.d.ts CHANGED
@@ -1,3 +1,8 @@
1
1
  export declare function range(min: number, max: number): number[];
2
2
  export declare function toDegrees(radians: number): number;
3
3
  export declare function toRadians(degrees: number): number;
4
+ export declare const formatCompact: {
5
+ (value: number): string;
6
+ (value: number | bigint): string;
7
+ (value: number | bigint | Intl.StringNumericLiteral): string;
8
+ };
package/dist/numbers.js CHANGED
@@ -11,3 +11,5 @@ export function toDegrees(radians) {
11
11
  export function toRadians(degrees) {
12
12
  return (degrees / 180) * Math.PI;
13
13
  }
14
+ const __formatter = Intl.NumberFormat('en', { notation: 'compact' });
15
+ export const formatCompact = __formatter.format.bind(__formatter);
package/dist/objects.d.ts CHANGED
@@ -20,3 +20,5 @@ export type Entries<T extends object> = ({
20
20
  export declare function isJSON(str: string): boolean;
21
21
  export declare function resolveConstructors(object: object): string[];
22
22
  export declare function map<const T extends Partial<Record<any, any>>>(items: T): Map<keyof T, T[keyof T]>;
23
+ export declare function getByString(object: Record<string, any>, path: string, separator?: RegExp): Record<string, any>;
24
+ export declare function setByString(object: Record<string, any>, path: string, value: unknown, separator?: RegExp): Record<string, any>;
package/dist/objects.js CHANGED
@@ -44,3 +44,15 @@ export function resolveConstructors(object) {
44
44
  export function map(items) {
45
45
  return new Map(Object.entries(items));
46
46
  }
47
+ export function getByString(object, path, separator = /[.[\]'"]/) {
48
+ return path
49
+ .split(separator)
50
+ .filter(p => p)
51
+ .reduce((o, p) => o?.[p], object);
52
+ }
53
+ export function setByString(object, path, value, separator = /[.[\]'"]/) {
54
+ return path
55
+ .split(separator)
56
+ .filter(p => p)
57
+ .reduce((o, p, i) => (o[p] = path.split(separator).filter(p => p).length === ++i ? value : o[p] || {}), object);
58
+ }
package/dist/shell.d.ts CHANGED
@@ -34,4 +34,4 @@ export interface ShellContext extends Required<ShellOptions> {
34
34
  /**
35
35
  * Creates a new shell using the provided options
36
36
  */
37
- export declare function createShell({ terminal, prompt, onLine }: ShellOptions): ShellContext;
37
+ export declare function createShell(options: ShellOptions): ShellContext;
package/dist/shell.js CHANGED
@@ -64,16 +64,18 @@ function handleData($, data) {
64
64
  /**
65
65
  * Creates a new shell using the provided options
66
66
  */
67
- export function createShell({ terminal, prompt = '', onLine = () => { } }) {
67
+ export function createShell(options) {
68
68
  const context = {
69
- terminal,
70
- prompt,
71
- onLine,
69
+ terminal: options.terminal,
70
+ get prompt() {
71
+ return options.prompt ?? '';
72
+ },
73
+ onLine: options.onLine ?? (() => { }),
72
74
  input: '',
73
75
  index: -1,
74
76
  currentInput: '',
75
77
  inputs: [],
76
78
  };
77
- terminal.onData(data => handleData(context, data));
79
+ options.terminal.onData(data => handleData(context, data));
78
80
  return context;
79
81
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utilium",
3
- "version": "0.8.2",
3
+ "version": "0.8.4",
4
4
  "description": "Typescript utilities",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/dom.ts CHANGED
@@ -30,3 +30,14 @@ export function download(data: BlobPart, name: string): void {
30
30
  link.download = name;
31
31
  link.click();
32
32
  }
33
+
34
+ /**
35
+ * Create an instance of a `<template>`
36
+ */
37
+ export function cloneTemplate(selector: string): DocumentFragment {
38
+ const template = document.querySelector<HTMLTemplateElement>(selector);
39
+ if (!template) {
40
+ throw new ReferenceError('Template does not exist: ' + selector);
41
+ }
42
+ return template.content.cloneNode(true) as DocumentFragment;
43
+ }
package/src/numbers.ts CHANGED
@@ -13,3 +13,7 @@ export function toDegrees(radians: number): number {
13
13
  export function toRadians(degrees: number): number {
14
14
  return (degrees / 180) * Math.PI;
15
15
  }
16
+
17
+ const __formatter = Intl.NumberFormat('en', { notation: 'compact' });
18
+
19
+ export const formatCompact = __formatter.format.bind(__formatter);
package/src/objects.ts CHANGED
@@ -64,3 +64,17 @@ export function resolveConstructors(object: object): string[] {
64
64
  export function map<const T extends Partial<Record<any, any>>>(items: T): Map<keyof T, T[keyof T]> {
65
65
  return new Map(Object.entries(items) as [keyof T, T[keyof T]][]);
66
66
  }
67
+
68
+ export function getByString(object: Record<string, any>, path: string, separator = /[.[\]'"]/) {
69
+ return path
70
+ .split(separator)
71
+ .filter(p => p)
72
+ .reduce((o, p) => o?.[p], object);
73
+ }
74
+
75
+ export function setByString(object: Record<string, any>, path: string, value: unknown, separator = /[.[\]'"]/) {
76
+ return path
77
+ .split(separator)
78
+ .filter(p => p)
79
+ .reduce((o, p, i) => (o[p] = path.split(separator).filter(p => p).length === ++i ? value : o[p] || {}), object);
80
+ }
package/src/shell.ts CHANGED
@@ -108,16 +108,18 @@ function handleData($: ShellContext, data: string) {
108
108
  /**
109
109
  * Creates a new shell using the provided options
110
110
  */
111
- export function createShell({ terminal, prompt = '', onLine = () => {} }: ShellOptions): ShellContext {
111
+ export function createShell(options: ShellOptions): ShellContext {
112
112
  const context: ShellContext = {
113
- terminal,
114
- prompt,
115
- onLine,
113
+ terminal: options.terminal,
114
+ get prompt() {
115
+ return options.prompt ?? '';
116
+ },
117
+ onLine: options.onLine ?? (() => {}),
116
118
  input: '',
117
119
  index: -1,
118
120
  currentInput: '',
119
121
  inputs: [],
120
122
  };
121
- terminal.onData(data => handleData(context, data));
123
+ options.terminal.onData(data => handleData(context, data));
122
124
  return context;
123
125
  }