@jackens/nnn 2025.2.4 → 2025.3.5

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 (4) hide show
  1. package/nnn.d.ts +15 -13
  2. package/nnn.js +21 -15
  3. package/package.json +1 -1
  4. package/readme.md +22 -67
package/nnn.d.ts CHANGED
@@ -105,19 +105,21 @@ export declare const svgUse: (id: string, ...args: Partial<Array<HArgs1>>) => SV
105
105
  */
106
106
  export declare const hasOwn: (ref: unknown, key: unknown) => boolean;
107
107
  /**
108
- * A helper that checks if the given argument is of a certain type.
109
- */
110
- export declare const is: {
111
- (type: ArrayConstructor, arg: unknown): arg is Partial<Array<unknown>>;
112
- (type: BigIntConstructor, arg: unknown): arg is bigint;
113
- (type: BooleanConstructor, arg: unknown): arg is boolean;
114
- (type: NumberConstructor, arg: unknown): arg is number;
115
- (type: ObjectConstructor, arg: unknown): arg is Partial<Record<PropertyKey, unknown>>;
116
- (type: StringConstructor, arg: unknown): arg is string;
117
- (type: SymbolConstructor, arg: unknown): arg is symbol;
118
- (type: undefined, arg: unknown): arg is undefined | null;
119
- <T extends abstract new (...args: Partial<Array<any>>) => unknown>(type: T, arg: unknown): arg is InstanceType<T>;
120
- };
108
+ * A helper that checks if the given argument is of type `any[]`.
109
+ */
110
+ export declare const isArray: (arg: any) => arg is any[];
111
+ /**
112
+ * A helper that checks if the given argument is of type `number`.
113
+ */
114
+ export declare const isNumber: (arg: any) => arg is number;
115
+ /**
116
+ * A helper that checks if the given argument is of type `Partial<Record<PropertyKey, unknown>>`.
117
+ */
118
+ export declare const isRecord: (arg: any) => arg is Partial<Record<PropertyKey, unknown>>;
119
+ /**
120
+ * A helper that checks if the given argument is of type `string`.
121
+ */
122
+ export declare const isString: (arg: any) => arg is string;
121
123
  /**
122
124
  * `JSON.parse` with “JavaScript turned on”.
123
125
  *
package/nnn.js CHANGED
@@ -1,5 +1,8 @@
1
1
  // src/nnn/is.ts
2
- var is = (type, arg) => arg?.constructor === type;
2
+ var isArray = Array.isArray;
3
+ var isNumber = (arg) => typeof arg === "number";
4
+ var isRecord = (arg) => typeof arg === "object" && arg != null && !isArray(arg);
5
+ var isString = (arg) => typeof arg === "string";
3
6
 
4
7
  // src/nnn/c.ts
5
8
  var _c = (node, prefix, result, split) => {
@@ -9,7 +12,7 @@ var _c = (node, prefix, result, split) => {
9
12
  if (style0 == null || prefix0 == null) {
10
13
  continue;
11
14
  }
12
- if (is(Array, style0)) {
15
+ if (isArray(style0)) {
13
16
  result.push(prefix0, prefix0 !== "" ? "{" : "", style0.join(";"), prefix0 !== "" ? "}" : "");
14
17
  } else {
15
18
  const todo = [];
@@ -17,7 +20,7 @@ var _c = (node, prefix, result, split) => {
17
20
  let attributesPushed = false;
18
21
  for (const key in style0) {
19
22
  const value = style0[key];
20
- if (is(String, value) || is(Number, value)) {
23
+ if (isString(value) || isNumber(value)) {
21
24
  if (!attributesPushed) {
22
25
  attributesPushed = true;
23
26
  attributes = [];
@@ -77,21 +80,19 @@ var NS = {
77
80
  var _h = (namespaceURI) => {
78
81
  const createElement = namespaceURI == null ? (tag) => document.createElement(tag) : (tag) => document.createElementNS(namespaceURI, tag);
79
82
  const h = (tagOrNode, ...args) => {
80
- const node = is(String, tagOrNode) ? createElement(tagOrNode) : tagOrNode;
83
+ const node = isString(tagOrNode) ? createElement(tagOrNode) : tagOrNode;
81
84
  args.forEach((arg) => {
82
85
  let child = null;
83
86
  if (arg instanceof Node) {
84
87
  child = arg;
85
- } else if (is(String, arg) || is(Number, arg)) {
86
- child = document.createTextNode(arg);
87
- } else if (is(Array, arg)) {
88
+ } else if (isArray(arg)) {
88
89
  child = h(...arg);
89
- } else if (arg != null) {
90
+ } else if (isRecord(arg)) {
90
91
  for (const name in arg) {
91
92
  const value = arg[name];
92
93
  if (name[0] === "$") {
93
94
  const name1 = name.slice(1);
94
- if (is(Object, value)) {
95
+ if (isRecord(value)) {
95
96
  node[name1] = node[name1] ?? {};
96
97
  Object.assign(node[name1], value);
97
98
  } else {
@@ -108,7 +109,7 @@ var _h = (namespaceURI) => {
108
109
  } else if (value === false) {
109
110
  node.removeAttributeNS(ns, basename);
110
111
  } else {
111
- node.setAttributeNS(ns, basename, is(String, value) ? value : "" + value);
112
+ node.setAttributeNS(ns, basename, value);
112
113
  }
113
114
  }
114
115
  } else {
@@ -117,11 +118,13 @@ var _h = (namespaceURI) => {
117
118
  } else if (value === false) {
118
119
  node.removeAttribute(name);
119
120
  } else {
120
- node.setAttribute(name, is(String, value) ? value : "" + value);
121
+ node.setAttribute(name, "" + value);
121
122
  }
122
123
  }
123
124
  }
124
125
  }
126
+ } else if (arg != null) {
127
+ child = document.createTextNode(arg);
125
128
  }
126
129
  if (child != null) {
127
130
  node.appendChild(child);
@@ -170,7 +173,7 @@ var fixTypography = (node) => {
170
173
  var hasOwn = (ref, key) => ref != null && Object.hasOwn(ref, key);
171
174
  // src/nnn/jsOnParse.ts
172
175
  var jsOnParse = (handlers, text) => JSON.parse(text, (key, value) => {
173
- if (is(Object, value)) {
176
+ if (isRecord(value)) {
174
177
  let isSecondKey = false;
175
178
  for (key in value) {
176
179
  if (isSecondKey) {
@@ -180,7 +183,7 @@ var jsOnParse = (handlers, text) => JSON.parse(text, (key, value) => {
180
183
  }
181
184
  const handler = handlers[key];
182
185
  const params = value[key];
183
- if (handler instanceof Function && is(Array, params)) {
186
+ if (handler instanceof Function && isArray(params)) {
184
187
  return handler(...params);
185
188
  }
186
189
  }
@@ -190,7 +193,7 @@ var jsOnParse = (handlers, text) => JSON.parse(text, (key, value) => {
190
193
  var locale = (map, defaultVersion) => (text, version = defaultVersion) => {
191
194
  const textV = map?.[version]?.[text];
192
195
  const textD = map?.[defaultVersion]?.[text];
193
- return is(String, textV) ? textV : is(String, textD) ? textD : text;
196
+ return isString(textV) ? textV : isString(textD) ? textD : text;
194
197
  };
195
198
  // src/nnn/nanolight.ts
196
199
  var nanolight = (pattern, highlighters, code) => {
@@ -249,7 +252,10 @@ export {
249
252
  nanolight,
250
253
  locale,
251
254
  jsOnParse,
252
- is,
255
+ isString,
256
+ isRecord,
257
+ isNumber,
258
+ isArray,
253
259
  hasOwn,
254
260
  h,
255
261
  fixTypography,
package/package.json CHANGED
@@ -36,5 +36,5 @@
36
36
  "name": "@jackens/nnn",
37
37
  "type": "module",
38
38
  "types": "nnn.d.ts",
39
- "version": "2025.2.4"
39
+ "version": "2025.3.5"
40
40
  }
package/readme.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Jackens’ JavaScript helpers.
4
4
 
5
- <sub>Version: <code class="version">2025.2.4</code></sub>
5
+ <sub>Version: <code class="version">2025.3.5</code></sub>
6
6
 
7
7
  * [Documentation](https://jackens.github.io/nnn/doc/)
8
8
  * [Tests](https://jackens.github.io/nnn/test/)
@@ -37,7 +37,7 @@ import { «something» } from './node_modules/@jackens/nnn/nnn.js'
37
37
  or
38
38
 
39
39
  ```js
40
- import { «something» } from 'https://unpkg.com/@jackens/nnn@2025.2.4/nnn.js'
40
+ import { «something» } from 'https://unpkg.com/@jackens/nnn@2025.3.5/nnn.js'
41
41
  ```
42
42
 
43
43
  ## Exports
@@ -54,7 +54,10 @@ import { «something» } from 'https://unpkg.com/@jackens/nnn@2025.2.4/nnn.js'
54
54
  - `fixTypography`: A helper that implements typographic corrections specific to Polish typography.
55
55
  - `h`: A lightweight [HyperScript](https://github.com/hyperhype/hyperscript)-style helper for creating and modifying `HTMLElement`s (see also `s`).
56
56
  - `hasOwn`: A replacement for the `in` operator (not to be confused with the `for-in` loop) that works properly.
57
- - `is`: A helper that checks if the given argument is of a certain type.
57
+ - `isArray`: A helper that checks if the given argument is of type `any[]`.
58
+ - `isNumber`: A helper that checks if the given argument is of type `number`.
59
+ - `isRecord`: A helper that checks if the given argument is of type `Partial<Record<PropertyKey, unknown>>`.
60
+ - `isString`: A helper that checks if the given argument is of type `string`.
58
61
  - `jsOnParse`: `JSON.parse` with “JavaScript turned on”.
59
62
  - `locale`: Language translations helper.
60
63
  - `nanolight`: A generic helper for syntax highlighting (see also `nanolightJs`).
@@ -565,86 +568,38 @@ expect(() => 'key' in undefined).to.throw
565
568
  expect(hasOwn(undefined, 'key')).to.be.false
566
569
  ```
567
570
 
568
- ### is
571
+ ### isArray
569
572
 
570
573
  ```ts
571
- const is: {
572
- (type: ArrayConstructor, arg: unknown): arg is Partial<Array<unknown>>;
573
- (type: BigIntConstructor, arg: unknown): arg is bigint;
574
- (type: BooleanConstructor, arg: unknown): arg is boolean;
575
- (type: NumberConstructor, arg: unknown): arg is number;
576
- (type: ObjectConstructor, arg: unknown): arg is Partial<Record<PropertyKey, unknown>>;
577
- (type: StringConstructor, arg: unknown): arg is string;
578
- (type: SymbolConstructor, arg: unknown): arg is symbol;
579
- (type: undefined, arg: unknown): arg is undefined | null;
580
- <T extends abstract new (...args: Partial<Array<any>>) => unknown>(type: T, arg: unknown): arg is InstanceType<T>;
581
- };
574
+ const isArray: (arg: any) => arg is any[];
582
575
  ```
583
576
 
584
- A helper that checks if the given argument is of a certain type.
585
-
586
- #### Usage Examples
587
-
588
- ```js
589
- expect(is(Number, 42)).to.be.true
590
- expect(is(Number, Number(42))).to.be.true
591
- expect(is(Number, new Number(42))).to.be.true
592
- expect(is(Number, NaN)).to.be.true
593
-
594
- expect(is(String, '42')).to.be.true
595
- expect(is(String, String('42'))).to.be.true
596
- expect(is(String, new String('42'))).to.be.true
597
-
598
- expect(is(Symbol, Symbol('42'))).to.be.true
599
- expect(is(Symbol, Object(Symbol('42')))).to.be.true
577
+ A helper that checks if the given argument is of type `any[]`.
600
578
 
601
- expect(is(undefined, undefined)).to.be.true
602
- expect(is(undefined, null)).to.be.true
579
+ ### isNumber
603
580
 
604
- expect(is(Object, {})).to.be.true
605
- expect(is(Array, [])).to.be.true
606
- expect(is(RegExp, /42/)).to.be.true
607
- expect(is(Date, new Date(42))).to.be.true
608
- expect(is(Set, new Set(['42', 42]))).to.be.true
609
- expect(is(Map, new Map([[{ j: 42 }, { J: '42' }], [{ c: 42 }, { C: '42' }]]))).to.be.true
581
+ ```ts
582
+ const isNumber: (arg: any) => arg is number;
610
583
  ```
611
584
 
612
- ```js
613
- const iz = (/** @type {unknown} */ type, /** @type {unknown} */ arg) =>
614
- ({}).toString.call(arg).slice(8, -1) === type?.name
615
-
616
- class FooBar { }
585
+ A helper that checks if the given argument is of type `number`.
617
586
 
618
- expect(is(FooBar, new FooBar())).to.be.true
619
- expect(iz(FooBar, new FooBar())).to.be.false
587
+ ### isRecord
620
588
 
621
- expect(is(Object, new FooBar())).to.be.false
622
- expect(iz(Object, new FooBar())).to.be.true
623
-
624
- const fakeFooBar = {}
625
-
626
- fakeFooBar[Symbol.toStringTag] = FooBar.name
627
-
628
- expect(is(FooBar, fakeFooBar)).to.be.false
629
- expect(iz(FooBar, fakeFooBar)).to.be.true
630
-
631
- expect(is(Object, fakeFooBar)).to.be.true
632
- expect(iz(Object, fakeFooBar)).to.be.false
589
+ ```ts
590
+ const isRecord: (arg: any) => arg is Partial<Record<PropertyKey, unknown>>;
633
591
  ```
634
592
 
635
- ```js
636
- const num = 42
637
- const str = '42'
638
-
639
- expect(is(Number, num)).to.be.true
593
+ A helper that checks if the given argument is of type `Partial<Record<PropertyKey, unknown>>`.
640
594
 
641
- try {
642
- num.constructor = str.constructor
643
- } catch { /* empty */ }
595
+ ### isString
644
596
 
645
- expect(is(Number, num)).to.be.true
597
+ ```ts
598
+ const isString: (arg: any) => arg is string;
646
599
  ```
647
600
 
601
+ A helper that checks if the given argument is of type `string`.
602
+
648
603
  ### jsOnParse
649
604
 
650
605
  ```ts