@jackens/nnn 2025.2.1 → 2025.2.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.
Files changed (4) hide show
  1. package/nnn.d.ts +2 -11
  2. package/nnn.js +4 -26
  3. package/package.json +1 -1
  4. package/readme.md +27 -75
package/nnn.d.ts CHANGED
@@ -103,7 +103,7 @@ export declare const svgUse: (id: string, ...args: Partial<Array<HArgs1>>) => SV
103
103
  /**
104
104
  * A replacement for the `in` operator (not to be confused with the `for-in` loop) that works properly.
105
105
  */
106
- export declare const has: (key: unknown, ref: unknown) => boolean;
106
+ export declare const hasOwn: (ref: unknown, key: unknown) => boolean;
107
107
  /**
108
108
  * A helper that checks if the given argument is of a certain type.
109
109
  */
@@ -163,18 +163,9 @@ export declare const plUral: (singular: string, plural2: string, plural5: string
163
163
  * object.
164
164
  */
165
165
  export declare const pro: (ref: unknown) => any;
166
- /**
167
- * A helper that provides information about the given `refs`.
168
- *
169
- * It returns an array of triples: `[«name», «prototype-name», «array-of-own-property-names»]`.
170
- */
171
- export declare const refsInfo: (...refs: Partial<Array<unknown>>) => Partial<Array<[string, string, Partial<Array<string>>]>>;
172
166
  /**
173
167
  * A helper that generates a UUID v1 identifier (with a creation timestamp).
174
168
  *
175
169
  * - The optional `node` parameter should have the format `/^[0123456789abcdef]+$/`. Its value will be trimmed to last 12 characters and left padded with zeros.
176
170
  */
177
- export declare const uuid1: ({ date, node }?: {
178
- date?: Date | undefined;
179
- node?: string | undefined;
180
- }) => string;
171
+ export declare const uuid1: (date?: Date, node?: string) => string;
package/nnn.js CHANGED
@@ -166,8 +166,8 @@ var fixTypography = (node) => {
166
166
  }
167
167
  }
168
168
  };
169
- // src/nnn/has.ts
170
- var has = (key, ref) => ref != null && Object.hasOwnProperty.call(ref, key);
169
+ // src/nnn/hasOwn.ts
170
+ var hasOwn = (ref, key) => ref != null && Object.hasOwn(ref, key);
171
171
  // src/nnn/jsOnParse.ts
172
172
  var jsOnParse = (handlers, text) => JSON.parse(text, (key, value) => {
173
173
  if (is(Object, value)) {
@@ -229,31 +229,10 @@ var pro = (ref) => new Proxy(ref, {
229
229
  return pro(target[key] = target[key] ?? {});
230
230
  }
231
231
  });
232
- // src/nnn/refsInfo.ts
233
- var refsInfo = (...refs) => {
234
- const fns = new Set;
235
- refs.forEach((ref) => {
236
- try {
237
- while (ref instanceof Function && !fns.has(ref) && `${ref}`.match(/function\s+\w+[\s\S]+\[native code\]/)) {
238
- fns.add(ref);
239
- ref = Object.getPrototypeOf(ref);
240
- }
241
- } catch {
242
- }
243
- });
244
- return Array.from(fns.values()).map((fn) => [
245
- fn.name,
246
- Object.getPrototypeOf(fn)?.name ?? "",
247
- Object.getOwnPropertyNames(fn.prototype ?? Object.create(null)).sort()
248
- ]).sort((a, b) => -(a[0] < b[0]));
249
- };
250
232
  // src/nnn/uuid1.ts
251
233
  var ZEROS = "0".repeat(16);
252
234
  var counter = 0;
253
- var uuid1 = ({
254
- date = new Date,
255
- node = Math.random().toString(16).slice(2)
256
- } = {}) => {
235
+ var uuid1 = (date = new Date, node = Math.random().toString(16).slice(2)) => {
257
236
  const time = ZEROS + (1e4 * (+date + 12219292800000)).toString(16);
258
237
  counter = counter + 1 & 16383;
259
238
  return time.slice(-8).concat("-", time.slice(-12, -8), -1, time.slice(-15, -12), "-", (8 | counter >> 12).toString(16), (ZEROS + (counter & 4095).toString(16)).slice(-3), "-", (ZEROS + node).slice(-12));
@@ -262,7 +241,6 @@ export {
262
241
  uuid1,
263
242
  svgUse,
264
243
  s,
265
- refsInfo,
266
244
  pro,
267
245
  plUral,
268
246
  pick,
@@ -272,7 +250,7 @@ export {
272
250
  locale,
273
251
  jsOnParse,
274
252
  is,
275
- has,
253
+ hasOwn,
276
254
  h,
277
255
  fixTypography,
278
256
  escapeValues,
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.1"
39
+ "version": "2025.2.4"
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.1</code></sub>
5
+ <sub>Version: <code class="version">2025.2.4</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.1/nnn.js'
40
+ import { «something» } from 'https://unpkg.com/@jackens/nnn@2025.2.4/nnn.js'
41
41
  ```
42
42
 
43
43
  ## Exports
@@ -53,7 +53,7 @@ import { «something» } from 'https://unpkg.com/@jackens/nnn@2025.2.1/nnn.js'
53
53
  - `escapeValues`: A generic helper for escaping `values` by given `escapeMap`.
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
- - `has`: A replacement for the `in` operator (not to be confused with the `for-in` loop) that works properly.
56
+ - `hasOwn`: A replacement for the `in` operator (not to be confused with the `for-in` loop) that works properly.
57
57
  - `is`: A helper that checks if the given argument is of a certain type.
58
58
  - `jsOnParse`: `JSON.parse` with “JavaScript turned on”.
59
59
  - `locale`: Language translations helper.
@@ -63,7 +63,6 @@ import { «something» } from 'https://unpkg.com/@jackens/nnn@2025.2.1/nnn.js'
63
63
  - `pick`: A helper that implements TypeScript’s `Pick` utility type (see also `omit`).
64
64
  - `plUral`: A helper for choosing the correct singular and plural.
65
65
  - `pro`: A helper that protects calls to nested properties by a `Proxy` that initializes non-existent values with an empty
66
- - `refsInfo`: A helper that provides information about the given `refs`.
67
66
  - `s`: A lightweight [HyperScript](https://github.com/hyperhype/hyperscript)-style helper for creating and modifying `SVGElement`s (see also `h`).
68
67
  - `svgUse`: A convenient shortcut for `s('svg', ['use', { 'xlink:href': '#' + id }], ...args)`.
69
68
  - `uuid1`: A helper that generates a UUID v1 identifier (with a creation timestamp).
@@ -525,10 +524,10 @@ h(div, { $key: { two: 2 } })
525
524
  expect(div.key).to.deep.equal({ one: 1, two: 2 })
526
525
  ```
527
526
 
528
- ### has
527
+ ### hasOwn
529
528
 
530
529
  ```ts
531
- const has: (key: unknown, ref: unknown) => boolean;
530
+ const hasOwn: (ref: unknown, key: unknown) => boolean;
532
531
  ```
533
532
 
534
533
  A replacement for the `in` operator (not to be confused with the `for-in` loop) that works properly.
@@ -536,35 +535,34 @@ A replacement for the `in` operator (not to be confused with the `for-in` loop)
536
535
  #### Usage Examples
537
536
 
538
537
  ```js
539
- const obj = { 'k,e,y': 42, null: 42 }
538
+ const obj = { 42: null, null: 'k,e,y', 'k,e,y': 42 }
540
539
 
541
- expect('k,e,y' in obj).to.be.true
542
- expect(has('k,e,y', obj)).to.be.true
540
+ expect(42 in obj).to.be.true
541
+ expect(hasOwn(obj, 42)).to.be.true
543
542
 
544
- expect(['k', 'e', 'y'] in obj).to.be.true
545
- expect(has(['k', 'e', 'y'], obj)).to.be.true
543
+ expect('42' in obj).to.be.true
544
+ expect(hasOwn(obj, '42')).to.be.true
546
545
 
547
546
  expect('null' in obj).to.be.true
548
- expect(has('null', obj)).to.be.true
547
+ expect(hasOwn(obj, 'null')).to.be.true
549
548
 
550
549
  expect(null in obj).to.be.true
551
- expect(has(null, obj)).to.be.true
550
+ expect(hasOwn(obj, null)).to.be.true
552
551
 
553
- expect('toString' in obj).to.be.true
554
- expect(has('toString', obj)).to.be.false
555
- ```
552
+ expect('k,e,y' in obj).to.be.true
553
+ expect(hasOwn(obj, 'k,e,y')).to.be.true
556
554
 
557
- ```js
558
- let typeError
555
+ expect(['k', 'e', 'y'] in obj).to.be.true
556
+ expect(hasOwn(obj, ['k', 'e', 'y'])).to.be.true
559
557
 
560
- try {
561
- 'key' in null
562
- } catch (error) {
563
- typeError = error
564
- }
558
+ expect('toString' in obj).to.be.true
559
+ expect(hasOwn(obj, 'toString')).to.be.false
565
560
 
566
- expect(typeError instanceof TypeError) // Cannot use 'in' operator to search for 'key' in null
567
- expect(has('key', null)).to.be.false
561
+ expect(() => 'key' in null).to.throw
562
+ expect(hasOwn(null, 'key')).to.be.false
563
+
564
+ expect(() => 'key' in undefined).to.throw
565
+ expect(hasOwn(undefined, 'key')).to.be.false
568
566
  ```
569
567
 
570
568
  ### is
@@ -873,49 +871,6 @@ pro(ref).one.two.three.four = 1234
873
871
  expect(ref).to.deep.equal({ one: { two: { three: { four: 1234 } } } })
874
872
  ```
875
873
 
876
- ### refsInfo
877
-
878
- ```ts
879
- const refsInfo: (...refs: Partial<Array<unknown>>) => Partial<Array<[string, string, Partial<Array<string>>]>>;
880
- ```
881
-
882
- A helper that provides information about the given `refs`.
883
-
884
- It returns an array of triples: `[«name», «prototype-name», «array-of-own-property-names»]`.
885
-
886
- #### Usage Examples
887
-
888
- ```js
889
- const info = refsInfo(Array, Function)
890
-
891
- expect(info.find(item => item?.[0] === 'Array')?.[2]?.includes('length')).to.be.true
892
- expect(info.find(item => item?.[0] === 'Function')?.[2]?.includes('length')).to.be.true
893
- ```
894
-
895
- ```js
896
- const browserFingerprint = () => {
897
- const refs = Object.getOwnPropertyNames(window).map(name => window[name])
898
- const info = refsInfo(...refs)
899
- const json = JSON.stringify(info)
900
- const hash = Array(32).fill(0)
901
- let j = 0
902
-
903
- for (let i = 0; i < json.length; i++) {
904
- let charCode = json.charCodeAt(i)
905
-
906
- while (charCode > 0) {
907
- hash[j] = hash[j] ^ (charCode & 15)
908
- charCode >>= 4
909
- j = (j + 1) & 31
910
- }
911
- }
912
-
913
- return hash.map(x => x.toString(16)).join('')
914
- }
915
-
916
- console.log(browserFingerprint())
917
- ```
918
-
919
874
  ### s
920
875
 
921
876
  ```ts
@@ -947,10 +902,7 @@ A convenient shortcut for `s('svg', ['use', { 'xlink:href': '#' + id }], ...args
947
902
  ### uuid1
948
903
 
949
904
  ```ts
950
- const uuid1: ({ date, node }?: {
951
- date?: Date | undefined;
952
- node?: string | undefined;
953
- }) => string;
905
+ const uuid1: (date?: Date, node?: string) => string;
954
906
  ```
955
907
 
956
908
  A helper that generates a UUID v1 identifier (with a creation timestamp).
@@ -974,12 +926,12 @@ for (let i = 1; i <= 22136; ++i) {
974
926
  ```
975
927
 
976
928
  ```js
977
- expect(uuid1({ node: '000123456789abc' }).split('-')[4]).to.deep.equal('123456789abc')
978
- expect(uuid1({ node: '123456789' }).split('-')[4]).to.deep.equal('000123456789')
929
+ expect(uuid1(new Date(), '000123456789abc').split('-')[4]).to.deep.equal('123456789abc')
930
+ expect(uuid1(new Date(), '123456789').split('-')[4]).to.deep.equal('000123456789')
979
931
  ```
980
932
 
981
933
  ```js
982
- expect(uuid1({ date: new Date(323325000000) }).startsWith('c1399400-9a71-11bd')).to.be.true
934
+ expect(uuid1(new Date(323325000000)).startsWith('c1399400-9a71-11bd')).to.be.true
983
935
  ```
984
936
 
985
937
  ## Why Partial\<Array\> and Partial\<Record\>