@jackens/nnn 2024.4.7 → 2024.4.15

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 -15
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.15"
42
42
  }
package/readme.md CHANGED
@@ -2,14 +2,7 @@
2
2
 
3
3
  Jackens’ JavaScript helpers.
4
4
 
5
- <sub>Version: <code class="version">2024.4.7</code></sub>
6
-
7
- ## Examples
8
-
9
- - [Documentation](https://jackens.github.io/nnn/doc/)
10
- - [Chessboard Demo](https://jackens.github.io/nnn/chessboard/)
11
- - [Gant Chart Demo](https://jackens.github.io/nnn/gantt/)
12
- - [Responsive Web Design Demo](https://jackens.github.io/nnn/rwd/)
5
+ <sub>Version: <code class="version">2024.4.15</code></sub>
13
6
 
14
7
  ## Installation
15
8
 
@@ -87,6 +80,8 @@ The type of arguments of the `h` and `s` helpers.
87
80
  type HArgs1 = Partial<Record<PropertyKey, unknown>> | null | undefined | Node | string | number | HArgs;
88
81
  ```
89
82
 
83
+ > See [here](https://jackens.github.io/nnn/doc/#Why-Partial-Array-and-Partial-Record) for an explanation of why `Partial<Record>` instead of `Record`.
84
+
90
85
  The type of arguments of the `h` and `s` helpers.
91
86
 
92
87
  ### JcNode
@@ -105,6 +100,8 @@ The type of arguments of the `jc` helper.
105
100
  type JcRoot = Partial<Record<PropertyKey, JcNode>>;
106
101
  ```
107
102
 
103
+ > See [here](https://jackens.github.io/nnn/doc/#Why-Partial-Array-and-Partial-Record) for an explanation of why `Partial<Record>` instead of `Record`.
104
+
108
105
  The type of arguments of the `jc` helper.
109
106
 
110
107
  ### csvParse
@@ -116,6 +113,8 @@ const csvParse: (text: string, { header, separator }?: {
116
113
  }) => Partial<Array<Partial<Record<PropertyKey, string>>>> | Partial<Array<Partial<Array<string>>>>;
117
114
  ```
118
115
 
116
+ > 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`.
117
+
119
118
  A tiny helper for CSV parsing.
120
119
 
121
120
  Options:
@@ -130,13 +129,13 @@ const text = `"aaa
130
129
  aaa",bbb, "ccc,ccc"
131
130
  "xxx,xxx", "yyy
132
131
  yyy",zzz
133
- 42,"42" , 17
132
+ 42 , "42" , 17
134
133
 
135
134
  `
136
135
  expect(csvParse(text, { header: false })).toStrictEqual([
137
136
  ['aaa\n"aaa"\naaa', 'bbb', 'ccc,ccc'],
138
137
  ['xxx,xxx', 'yyy\nyyy', 'zzz'],
139
- ['42', '42', ' 17']
138
+ [' 42 ', '42', ' 17']
140
139
  ])
141
140
 
142
141
  expect(csvParse(text)).toStrictEqual([{
@@ -144,7 +143,7 @@ expect(csvParse(text)).toStrictEqual([{
144
143
  bbb: 'yyy\nyyy',
145
144
  'ccc,ccc': 'zzz'
146
145
  }, {
147
- 'aaa\n"aaa"\naaa': '42',
146
+ 'aaa\n"aaa"\naaa': ' 42 ',
148
147
  bbb: '42',
149
148
  'ccc,ccc': ' 17'
150
149
  }])
@@ -156,6 +155,8 @@ expect(csvParse(text)).toStrictEqual([{
156
155
  const escape: (escapeMap: EscapeMap, template: TemplateStringsArray, ...values: Partial<Array<unknown>>) => string;
157
156
  ```
158
157
 
158
+ > See [here](https://jackens.github.io/nnn/doc/#Why-Partial-Array-and-Partial-Record) for an explanation of why `Partial<Array>` instead of `Array`.
159
+
159
160
  A generic helper for escaping `values` by given `escapeMap` (in *TemplateStrings* flavor).
160
161
 
161
162
  #### Usage Examples
@@ -191,6 +192,8 @@ expect(actual).toStrictEqual(expected)
191
192
  const escapeValues: (escapeMap: EscapeMap, values: Partial<Array<unknown>>) => Partial<Array<string>>;
192
193
  ```
193
194
 
195
+ > See [here](https://jackens.github.io/nnn/doc/#Why-Partial-Array-and-Partial-Record) for an explanation of why `Partial<Array>` instead of `Array`.
196
+
194
197
  A generic helper for escaping `values` by given `escapeMap`.
195
198
 
196
199
  ### fixTypography
@@ -223,6 +226,8 @@ const h: {
223
226
  };
224
227
  ```
225
228
 
229
+ > See [here](https://jackens.github.io/nnn/doc/#Why-Partial-Array-and-Partial-Record) for an explanation of why `Partial<Array>` instead of `Array`.
230
+
226
231
  A lightweight [HyperScript](https://github.com/hyperhype/hyperscript)-style helper for creating and modifying `HTMLElement`s (see also `s`).
227
232
 
228
233
  - The first argument of type `string` specifies the tag of the element to be created.
@@ -360,6 +365,8 @@ const is: {
360
365
  };
361
366
  ```
362
367
 
368
+ > 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`.
369
+
363
370
  A helper that checks if the given argument is of a certain type.
364
371
 
365
372
  #### Usage Examples
@@ -637,6 +644,8 @@ expect(actual).toStrictEqual(expected)
637
644
  const jsOnParse: (handlers: Partial<Record<PropertyKey, Function>>, text: string) => any;
638
645
  ```
639
646
 
647
+ > See [here](https://jackens.github.io/nnn/doc/#Why-Partial-Array-and-Partial-Record) for an explanation of why `Partial<Record>` instead of `Record`.
648
+
640
649
  `JSON.parse` with “JavaScript turned on”.
641
650
 
642
651
  Objects having *exactly* one property which is present in the `handlers` map, i.e. objects of the form:
@@ -704,6 +713,8 @@ expect(actual).toStrictEqual(expected)
704
713
  const locale: (map: Partial<Record<PropertyKey, Partial<Record<PropertyKey, string>>>>, defaultVersion: string) => (text: string, version?: string) => string;
705
714
  ```
706
715
 
716
+ > See [here](https://jackens.github.io/nnn/doc/#Why-Partial-Array-and-Partial-Record) for an explanation of why `Partial<Record>` instead of `Record`.
717
+
707
718
  Language translations helper.
708
719
 
709
720
  #### Usage Examples
@@ -731,9 +742,11 @@ expect(_('toString', 'undefined_version')).toStrictEqual('toString')
731
742
  ### nanolight
732
743
 
733
744
  ```ts
734
- const nanolight: (pattern: RegExp, highlighters: ((chunk: string, index: number) => HArgs1)[], code: string) => HArgs1[];
745
+ const nanolight: (pattern: RegExp, highlighters: Partial<Array<(chunk: string, index: number) => HArgs1>>, code: string) => HArgs1[];
735
746
  ```
736
747
 
748
+ > See [here](https://jackens.github.io/nnn/doc/#Why-Partial-Array-and-Partial-Record) for an explanation of why `Partial<Array>` instead of `Array`.
749
+
737
750
  A generic helper for syntax highlighting (see also `nanolightJs`).
738
751
 
739
752
  ### nanolightJs
@@ -766,6 +779,8 @@ expect(nanolightJs(codeJs)).toStrictEqual([
766
779
  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
780
  ```
768
781
 
782
+ > 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`.
783
+
769
784
  A helper that implements TypeScript’s `Omit` utility type.
770
785
 
771
786
  #### Usage Examples
@@ -782,6 +797,8 @@ expect(omit(obj, ['c'])).toStrictEqual({ a: 42, b: '42' })
782
797
  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
798
  ```
784
799
 
800
+ > 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`.
801
+
785
802
  A helper that implements TypeScript’s `Pick` utility type.
786
803
 
787
804
  #### Usage Examples
@@ -859,9 +876,11 @@ expect(ref).toStrictEqual({ one: { two: { three: { four: 1234 } } } })
859
876
  ### refsInfo
860
877
 
861
878
  ```ts
862
- const refsInfo: (...refs: Partial<Array<unknown>>) => [string, string, (string | undefined)[]][];
879
+ const refsInfo: (...refs: Partial<Array<unknown>>) => Partial<Array<[string, string, Partial<Array<string>>]>>;
863
880
  ```
864
881
 
882
+ > See [here](https://jackens.github.io/nnn/doc/#Why-Partial-Array-and-Partial-Record) for an explanation of why `Partial<Array>` instead of `Array`.
883
+
865
884
  A helper that provides information about the given `refs`.
866
885
 
867
886
  It returns an array of triples: `[«name», «prototype-name», «array-of-own-property-names»]`.
@@ -871,8 +890,8 @@ It returns an array of triples: `[«name», «prototype-name», «array-of-own-p
871
890
  ```js
872
891
  const info = refsInfo(Array, Function)
873
892
 
874
- expect(info.find(([name]) => name === 'Array')?.[2]?.includes('length')).toBeTrue()
875
- expect(info.find(([name]) => name === 'Function')?.[2]?.includes('length')).toBeTrue()
893
+ expect(info.find(item => item?.[0] === 'Array')?.[2]?.includes('length')).toBeTrue()
894
+ expect(info.find(item => item?.[0] === 'Function')?.[2]?.includes('length')).toBeTrue()
876
895
  ```
877
896
 
878
897
  ```js
@@ -909,6 +928,8 @@ const s: {
909
928
  };
910
929
  ```
911
930
 
931
+ > See [here](https://jackens.github.io/nnn/doc/#Why-Partial-Array-and-Partial-Record) for an explanation of why `Partial<Array>` instead of `Array`.
932
+
912
933
  A lightweight [HyperScript](https://github.com/hyperhype/hyperscript)-style helper for creating and modifying `SVGElement`s (see also `h`).
913
934
 
914
935
  - The first argument of type `string` specifies the tag of the element to be created.
@@ -929,6 +950,8 @@ A lightweight [HyperScript](https://github.com/hyperhype/hyperscript)-style help
929
950
  const svgUse: (id: string, ...args: Partial<Array<HArgs1>>) => SVGSVGElement;
930
951
  ```
931
952
 
953
+ > See [here](https://jackens.github.io/nnn/doc/#Why-Partial-Array-and-Partial-Record) for an explanation of why `Partial<Array>` instead of `Array`.
954
+
932
955
  A convenient shortcut for `s('svg', ['use', { 'xlink:href': '#' + id }], ...args)`.
933
956
 
934
957
  ### uuid1
@@ -970,6 +993,52 @@ expect(uuid1({ node: '123456789' }).split('-')[4]).toStrictEqual('000123456789')
970
993
  expect(uuid1({ date: new Date(323325000000) }).startsWith('c1399400-9a71-11bd')).toBeTrue()
971
994
  ```
972
995
 
996
+ ## Why Partial\<Array\> and Partial\<Record\>
997
+
998
+ Consider the following code snippet:
999
+
1000
+ ```ts
1001
+ const arr = ['one', 'two', 'three'] // type: string[]
1002
+ const arrAt42 = arr[42] // type: string
1003
+
1004
+ arrAt42.toUpperCase() // 👎 allowed by TypeScript
1005
+ ```
1006
+
1007
+ TypeScript allows `arrAt42.toUpperCase()`,
1008
+ which causes a <code class="log">TypeError: undefined is not an object</code>.
1009
+ The variable `arr` should be of type `Partial<Array<string>>`:
1010
+
1011
+ ```ts
1012
+ const arr: Partial<Array<string>> = ['one', 'two', 'three']
1013
+ const arrAt42 = arr[42] // type: string | undefined
1014
+
1015
+ arrAt42.toUpperCase() // 👍 forbidden by TypeScript
1016
+ ```
1017
+
1018
+ Now `arrAt42.toUpperCase()` is forbidden by TypeScript (<code class="log">'arrAt42' is possibly undefined</code>).
1019
+
1020
+ Similarly for the type `Record`:
1021
+
1022
+ ```ts
1023
+ const rec = Object.fromEntries(['one', 'two', 'three'].map((k, i) => [k, i])) // type: Record<string, number>
1024
+ const { tree } = rec // type: number
1025
+
1026
+ tree.toFixed() // 👎 allowed by TypeScript
1027
+ ```
1028
+
1029
+ TypeScript allows `tree.toFixed()`, which causes a <code class="log">TypeError: undefined is not an object</code>.
1030
+ The variable `rec` should be of type `Partial<Record<PropertyKey, number>>`:
1031
+
1032
+ ```ts
1033
+ const rec: Partial<Record<PropertyKey, number>> =
1034
+ Object.fromEntries(['one', 'two', 'three'].map((k, i) => [k, i]))
1035
+ const { tree } = rec // type: number | undefined
1036
+
1037
+ tree.toFixed() // 👍 forbidden by TypeScript
1038
+ ```
1039
+
1040
+ Now `tree.toFixed()` is forbidden by TypeScript (<code class="log">'tree' is possibly undefined</code>).
1041
+
973
1042
  ## License
974
1043
 
975
1044
  The MIT License (MIT)