@jackens/nnn 2024.4.7 → 2024.4.14

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 (3) hide show
  1. package/nnn.d.ts +2 -2
  2. package/package.json +1 -1
  3. package/readme.md +84 -8
package/nnn.d.ts CHANGED
@@ -157,7 +157,7 @@ export declare const locale: (map: Partial<Record<PropertyKey, Partial<Record<Pr
157
157
  /**
158
158
  * A generic helper for syntax highlighting (see also `nanolightJs`).
159
159
  */
160
- export declare const nanolight: (pattern: RegExp, highlighters: ((chunk: string, index: number) => HArgs1)[], code: string) => HArgs1[];
160
+ export declare const nanolight: (pattern: RegExp, highlighters: Partial<Array<(chunk: string, index: number) => HArgs1>>, code: string) => HArgs1[];
161
161
 
162
162
  /**
163
163
  * A helper for highlighting JavaScript.
@@ -189,7 +189,7 @@ export declare const pro: (ref: unknown) => any;
189
189
  *
190
190
  * It returns an array of triples: `[«name», «prototype-name», «array-of-own-property-names»]`.
191
191
  */
192
- export declare const refsInfo: (...refs: Partial<Array<unknown>>) => [string, string, (string | undefined)[]][];
192
+ export declare const refsInfo: (...refs: Partial<Array<unknown>>) => Partial<Array<[string, string, Partial<Array<string>>]>>;
193
193
 
194
194
  /**
195
195
  * A helper that generates a UUID v1 identifier (with a creation timestamp).
package/package.json CHANGED
@@ -38,5 +38,5 @@
38
38
  "types": "nnn.d.ts",
39
39
  "name": "@jackens/nnn",
40
40
  "type": "module",
41
- "version": "2024.4.7"
41
+ "version": "2024.4.14"
42
42
  }
package/readme.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Jackens’ JavaScript helpers.
4
4
 
5
- <sub>Version: <code class="version">2024.4.7</code></sub>
5
+ <sub>Version: <code class="version">2024.4.14</code></sub>
6
6
 
7
7
  ## Examples
8
8
 
@@ -87,6 +87,8 @@ The type of arguments of the `h` and `s` helpers.
87
87
  type HArgs1 = Partial<Record<PropertyKey, unknown>> | null | undefined | Node | string | number | HArgs;
88
88
  ```
89
89
 
90
+ > See [here](https://jackens.github.io/nnn/doc/#Why-Partial-Array-and-Partial-Record) for an explanation of why `Partial<Record>` instead of `Record`.
91
+
90
92
  The type of arguments of the `h` and `s` helpers.
91
93
 
92
94
  ### JcNode
@@ -105,6 +107,8 @@ The type of arguments of the `jc` helper.
105
107
  type JcRoot = Partial<Record<PropertyKey, JcNode>>;
106
108
  ```
107
109
 
110
+ > See [here](https://jackens.github.io/nnn/doc/#Why-Partial-Array-and-Partial-Record) for an explanation of why `Partial<Record>` instead of `Record`.
111
+
108
112
  The type of arguments of the `jc` helper.
109
113
 
110
114
  ### csvParse
@@ -116,6 +120,8 @@ const csvParse: (text: string, { header, separator }?: {
116
120
  }) => Partial<Array<Partial<Record<PropertyKey, string>>>> | Partial<Array<Partial<Array<string>>>>;
117
121
  ```
118
122
 
123
+ > See [here](https://jackens.github.io/nnn/doc/#Why-Partial-Array-and-Partial-Record) for an explanation of why `Partial<Array>`/`Partial<Record>` instead of `Array`/`Record`.
124
+
119
125
  A tiny helper for CSV parsing.
120
126
 
121
127
  Options:
@@ -130,13 +136,13 @@ const text = `"aaa
130
136
  aaa",bbb, "ccc,ccc"
131
137
  "xxx,xxx", "yyy
132
138
  yyy",zzz
133
- 42,"42" , 17
139
+ 42 , "42" , 17
134
140
 
135
141
  `
136
142
  expect(csvParse(text, { header: false })).toStrictEqual([
137
143
  ['aaa\n"aaa"\naaa', 'bbb', 'ccc,ccc'],
138
144
  ['xxx,xxx', 'yyy\nyyy', 'zzz'],
139
- ['42', '42', ' 17']
145
+ [' 42 ', '42', ' 17']
140
146
  ])
141
147
 
142
148
  expect(csvParse(text)).toStrictEqual([{
@@ -144,7 +150,7 @@ expect(csvParse(text)).toStrictEqual([{
144
150
  bbb: 'yyy\nyyy',
145
151
  'ccc,ccc': 'zzz'
146
152
  }, {
147
- 'aaa\n"aaa"\naaa': '42',
153
+ 'aaa\n"aaa"\naaa': ' 42 ',
148
154
  bbb: '42',
149
155
  'ccc,ccc': ' 17'
150
156
  }])
@@ -156,6 +162,8 @@ expect(csvParse(text)).toStrictEqual([{
156
162
  const escape: (escapeMap: EscapeMap, template: TemplateStringsArray, ...values: Partial<Array<unknown>>) => string;
157
163
  ```
158
164
 
165
+ > See [here](https://jackens.github.io/nnn/doc/#Why-Partial-Array-and-Partial-Record) for an explanation of why `Partial<Array>` instead of `Array`.
166
+
159
167
  A generic helper for escaping `values` by given `escapeMap` (in *TemplateStrings* flavor).
160
168
 
161
169
  #### Usage Examples
@@ -191,6 +199,8 @@ expect(actual).toStrictEqual(expected)
191
199
  const escapeValues: (escapeMap: EscapeMap, values: Partial<Array<unknown>>) => Partial<Array<string>>;
192
200
  ```
193
201
 
202
+ > See [here](https://jackens.github.io/nnn/doc/#Why-Partial-Array-and-Partial-Record) for an explanation of why `Partial<Array>` instead of `Array`.
203
+
194
204
  A generic helper for escaping `values` by given `escapeMap`.
195
205
 
196
206
  ### fixTypography
@@ -223,6 +233,8 @@ const h: {
223
233
  };
224
234
  ```
225
235
 
236
+ > See [here](https://jackens.github.io/nnn/doc/#Why-Partial-Array-and-Partial-Record) for an explanation of why `Partial<Array>` instead of `Array`.
237
+
226
238
  A lightweight [HyperScript](https://github.com/hyperhype/hyperscript)-style helper for creating and modifying `HTMLElement`s (see also `s`).
227
239
 
228
240
  - The first argument of type `string` specifies the tag of the element to be created.
@@ -360,6 +372,8 @@ const is: {
360
372
  };
361
373
  ```
362
374
 
375
+ > See [here](https://jackens.github.io/nnn/doc/#Why-Partial-Array-and-Partial-Record) for an explanation of why `Partial<Array>`/`Partial<Record>` instead of `Array`/`Record`.
376
+
363
377
  A helper that checks if the given argument is of a certain type.
364
378
 
365
379
  #### Usage Examples
@@ -637,6 +651,8 @@ expect(actual).toStrictEqual(expected)
637
651
  const jsOnParse: (handlers: Partial<Record<PropertyKey, Function>>, text: string) => any;
638
652
  ```
639
653
 
654
+ > See [here](https://jackens.github.io/nnn/doc/#Why-Partial-Array-and-Partial-Record) for an explanation of why `Partial<Record>` instead of `Record`.
655
+
640
656
  `JSON.parse` with “JavaScript turned on”.
641
657
 
642
658
  Objects having *exactly* one property which is present in the `handlers` map, i.e. objects of the form:
@@ -704,6 +720,8 @@ expect(actual).toStrictEqual(expected)
704
720
  const locale: (map: Partial<Record<PropertyKey, Partial<Record<PropertyKey, string>>>>, defaultVersion: string) => (text: string, version?: string) => string;
705
721
  ```
706
722
 
723
+ > See [here](https://jackens.github.io/nnn/doc/#Why-Partial-Array-and-Partial-Record) for an explanation of why `Partial<Record>` instead of `Record`.
724
+
707
725
  Language translations helper.
708
726
 
709
727
  #### Usage Examples
@@ -731,9 +749,11 @@ expect(_('toString', 'undefined_version')).toStrictEqual('toString')
731
749
  ### nanolight
732
750
 
733
751
  ```ts
734
- const nanolight: (pattern: RegExp, highlighters: ((chunk: string, index: number) => HArgs1)[], code: string) => HArgs1[];
752
+ const nanolight: (pattern: RegExp, highlighters: Partial<Array<(chunk: string, index: number) => HArgs1>>, code: string) => HArgs1[];
735
753
  ```
736
754
 
755
+ > See [here](https://jackens.github.io/nnn/doc/#Why-Partial-Array-and-Partial-Record) for an explanation of why `Partial<Array>` instead of `Array`.
756
+
737
757
  A generic helper for syntax highlighting (see also `nanolightJs`).
738
758
 
739
759
  ### nanolightJs
@@ -766,6 +786,8 @@ expect(nanolightJs(codeJs)).toStrictEqual([
766
786
  const omit: <T extends Partial<Record<PropertyKey, unknown>>, K extends (keyof T)[]>(obj: Partial<Record<PropertyKey, unknown>>, keys: Partial<Array<unknown>>) => Omit<T, K[number]>;
767
787
  ```
768
788
 
789
+ > See [here](https://jackens.github.io/nnn/doc/#Why-Partial-Array-and-Partial-Record) for an explanation of why `Partial<Array>`/`Partial<Record>` instead of `Array`/`Record`.
790
+
769
791
  A helper that implements TypeScript’s `Omit` utility type.
770
792
 
771
793
  #### Usage Examples
@@ -782,6 +804,8 @@ expect(omit(obj, ['c'])).toStrictEqual({ a: 42, b: '42' })
782
804
  const pick: <T extends Partial<Record<PropertyKey, unknown>>, K extends (keyof T)[]>(obj: Partial<Record<PropertyKey, unknown>>, keys: Partial<Array<unknown>>) => Pick<T, K[number]>;
783
805
  ```
784
806
 
807
+ > See [here](https://jackens.github.io/nnn/doc/#Why-Partial-Array-and-Partial-Record) for an explanation of why `Partial<Array>`/`Partial<Record>` instead of `Array`/`Record`.
808
+
785
809
  A helper that implements TypeScript’s `Pick` utility type.
786
810
 
787
811
  #### Usage Examples
@@ -859,9 +883,11 @@ expect(ref).toStrictEqual({ one: { two: { three: { four: 1234 } } } })
859
883
  ### refsInfo
860
884
 
861
885
  ```ts
862
- const refsInfo: (...refs: Partial<Array<unknown>>) => [string, string, (string | undefined)[]][];
886
+ const refsInfo: (...refs: Partial<Array<unknown>>) => Partial<Array<[string, string, Partial<Array<string>>]>>;
863
887
  ```
864
888
 
889
+ > See [here](https://jackens.github.io/nnn/doc/#Why-Partial-Array-and-Partial-Record) for an explanation of why `Partial<Array>` instead of `Array`.
890
+
865
891
  A helper that provides information about the given `refs`.
866
892
 
867
893
  It returns an array of triples: `[«name», «prototype-name», «array-of-own-property-names»]`.
@@ -871,8 +897,8 @@ It returns an array of triples: `[«name», «prototype-name», «array-of-own-p
871
897
  ```js
872
898
  const info = refsInfo(Array, Function)
873
899
 
874
- expect(info.find(([name]) => name === 'Array')?.[2]?.includes('length')).toBeTrue()
875
- expect(info.find(([name]) => name === 'Function')?.[2]?.includes('length')).toBeTrue()
900
+ expect(info.find(item => item?.[0] === 'Array')?.[2]?.includes('length')).toBeTrue()
901
+ expect(info.find(item => item?.[0] === 'Function')?.[2]?.includes('length')).toBeTrue()
876
902
  ```
877
903
 
878
904
  ```js
@@ -909,6 +935,8 @@ const s: {
909
935
  };
910
936
  ```
911
937
 
938
+ > See [here](https://jackens.github.io/nnn/doc/#Why-Partial-Array-and-Partial-Record) for an explanation of why `Partial<Array>` instead of `Array`.
939
+
912
940
  A lightweight [HyperScript](https://github.com/hyperhype/hyperscript)-style helper for creating and modifying `SVGElement`s (see also `h`).
913
941
 
914
942
  - The first argument of type `string` specifies the tag of the element to be created.
@@ -929,6 +957,8 @@ A lightweight [HyperScript](https://github.com/hyperhype/hyperscript)-style help
929
957
  const svgUse: (id: string, ...args: Partial<Array<HArgs1>>) => SVGSVGElement;
930
958
  ```
931
959
 
960
+ > See [here](https://jackens.github.io/nnn/doc/#Why-Partial-Array-and-Partial-Record) for an explanation of why `Partial<Array>` instead of `Array`.
961
+
932
962
  A convenient shortcut for `s('svg', ['use', { 'xlink:href': '#' + id }], ...args)`.
933
963
 
934
964
  ### uuid1
@@ -970,6 +1000,52 @@ expect(uuid1({ node: '123456789' }).split('-')[4]).toStrictEqual('000123456789')
970
1000
  expect(uuid1({ date: new Date(323325000000) }).startsWith('c1399400-9a71-11bd')).toBeTrue()
971
1001
  ```
972
1002
 
1003
+ ## Why Partial\<Array\> and Partial\<Record\>
1004
+
1005
+ Consider the following code snippet:
1006
+
1007
+ ```ts
1008
+ const arr = ['one', 'two', 'three'] // type: string[]
1009
+ const arrAt42 = arr[42] // type: string
1010
+
1011
+ arrAt42.toUpperCase() // 👎 allowed by TypeScript
1012
+ ```
1013
+
1014
+ TypeScript allows `arrAt42.toUpperCase()`,
1015
+ which causes a <code class="log">TypeError: undefined is not an object</code>.
1016
+ The variable `arr` should be of type `Partial<Array<string>>`:
1017
+
1018
+ ```ts
1019
+ const arr: Partial<Array<string>> = ['one', 'two', 'three']
1020
+ const arrAt42 = arr[42] // type: string | undefined
1021
+
1022
+ arrAt42.toUpperCase() // 👍 forbidden by TypeScript
1023
+ ```
1024
+
1025
+ Now `arrAt42.toUpperCase()` is forbidden by TypeScript (<code class="log">'arrAt42' is possibly undefined</code>).
1026
+
1027
+ Similarly for the type `Record`:
1028
+
1029
+ ```ts
1030
+ const rec = Object.fromEntries(['one', 'two', 'three'].map((k, i) => [k, i])) // type: Record<string, number>
1031
+ const { tree } = rec // type: number
1032
+
1033
+ tree.toFixed() // 👎 allowed by TypeScript
1034
+ ```
1035
+
1036
+ TypeScript allows `tree.toFixed()`, which causes a <code class="log">TypeError: undefined is not an object</code>.
1037
+ The variable `rec` should be of type `Partial<Record<PropertyKey, number>>`:
1038
+
1039
+ ```ts
1040
+ const rec: Partial<Record<PropertyKey, number>> =
1041
+ Object.fromEntries(['one', 'two', 'three'].map((k, i) => [k, i]))
1042
+ const { tree } = rec // type: number | undefined
1043
+
1044
+ tree.toFixed() // 👍 forbidden by TypeScript
1045
+ ```
1046
+
1047
+ Now `tree.toFixed()` is forbidden by TypeScript (<code class="log">'tree' is possibly undefined</code>).
1048
+
973
1049
  ## License
974
1050
 
975
1051
  The MIT License (MIT)