jostraca 0.7.3 → 0.7.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 (54) hide show
  1. package/dist/cmp/Content.d.ts +1 -1
  2. package/dist/cmp/Content.js.map +1 -1
  3. package/dist/cmp/Copy.d.ts +1 -1
  4. package/dist/cmp/File.d.ts +1 -1
  5. package/dist/cmp/File.js +1 -1
  6. package/dist/cmp/File.js.map +1 -1
  7. package/dist/cmp/Folder.d.ts +1 -1
  8. package/dist/cmp/Folder.js +1 -1
  9. package/dist/cmp/Folder.js.map +1 -1
  10. package/dist/cmp/Fragment.d.ts +1 -1
  11. package/dist/cmp/Fragment.js +1 -0
  12. package/dist/cmp/Fragment.js.map +1 -1
  13. package/dist/cmp/Inject.d.ts +1 -1
  14. package/dist/cmp/Inject.js +1 -1
  15. package/dist/cmp/Inject.js.map +1 -1
  16. package/dist/cmp/Project.d.ts +1 -1
  17. package/dist/cmp/Project.js +1 -1
  18. package/dist/cmp/Project.js.map +1 -1
  19. package/dist/jostraca.d.ts +1 -1
  20. package/dist/jostraca.js +11 -10
  21. package/dist/jostraca.js.map +1 -1
  22. package/dist/op/CopyOp.js +16 -12
  23. package/dist/op/CopyOp.js.map +1 -1
  24. package/dist/op/FileOp.d.ts +1 -1
  25. package/dist/op/FileOp.js +11 -11
  26. package/dist/op/FileOp.js.map +1 -1
  27. package/dist/op/FragmentOp.js +4 -1
  28. package/dist/op/FragmentOp.js.map +1 -1
  29. package/dist/op/InjectOp.d.ts +1 -1
  30. package/dist/op/InjectOp.js +2 -2
  31. package/dist/op/InjectOp.js.map +1 -1
  32. package/dist/tsconfig.tsbuildinfo +1 -1
  33. package/dist/types.d.ts +26 -0
  34. package/dist/types.js +4 -0
  35. package/dist/types.js.map +1 -0
  36. package/dist/utility.d.ts +7 -25
  37. package/dist/utility.js +60 -26
  38. package/dist/utility.js.map +1 -1
  39. package/package.json +2 -2
  40. package/src/cmp/Content.ts +1 -2
  41. package/src/cmp/Copy.ts +1 -1
  42. package/src/cmp/File.ts +2 -2
  43. package/src/cmp/Folder.ts +2 -2
  44. package/src/cmp/Fragment.ts +2 -2
  45. package/src/cmp/Inject.ts +2 -2
  46. package/src/cmp/Project.ts +2 -2
  47. package/src/jostraca.ts +13 -12
  48. package/src/op/CopyOp.ts +17 -12
  49. package/src/op/FileOp.ts +11 -11
  50. package/src/op/FragmentOp.ts +6 -1
  51. package/src/op/InjectOp.ts +2 -2
  52. package/src/types.ts +47 -0
  53. package/src/utility.ts +82 -63
  54. package/gen/readme.js~ +0 -1
package/src/utility.ts CHANGED
@@ -1,81 +1,103 @@
1
1
  /* Copyright (c) 2024 Richard Rodger, MIT License */
2
2
 
3
3
 
4
- type JostracaOptions = {
5
- folder?: string
6
- fs?: any
7
- meta?: any
8
- }
9
-
4
+ // Iterate over arrays and objects (opinionated mutation!).
5
+ function each(
6
+ subject?: any[] | Object, // Iterate over subject.
7
+ flags?: { // Optional, flags to control output.
8
+ mark?: boolean, // Mark items (key$ or index$), default: true,
9
+ oval?: boolean, // Convert non-object values into synthetic objects, default: true
10
+ sort?: boolean | string, // Sort items, optionally by named prop, default: false
11
+ call?: boolean, // Call items, if they are functions, default: false
12
+ } | ((...a: any[]) => any),
13
+ apply?: (...a: any[]) => any) // Optional Function to apply to each item.
14
+ {
15
+ const isArray = Array.isArray(subject)
16
+
17
+ const hasFlags = null != flags && 'function' !== typeof flags
18
+ apply = (hasFlags ? apply : flags) as ((...a: any[]) => any)
19
+
20
+ flags = (hasFlags ? flags : {}) as { mark: boolean, sort: boolean }
21
+ flags.mark = null != flags.mark ? flags.mark : true
22
+ flags.oval = null != flags.oval ? flags.oval : true
23
+ flags.sort = null != flags.sort ? flags.sort : false
24
+ flags.call = null != flags.call ? flags.call : false
25
+
26
+ let out: any[] = []
27
+
28
+ if (isArray) {
29
+ for (let fn of subject) {
30
+ out.push(flags.call && 'function' === typeof fn ? fn() : fn)
31
+ }
10
32
 
11
- type Node = {
12
- kind: string
13
- children?: Node[]
14
- name?: string
15
- path: string[]
16
- from?: string
17
- content?: any[]
18
- folder?: string
19
- after?: any
20
- exclude?: boolean
21
- meta: any
22
- }
33
+ out = true === flags.sort && 1 < out.length ? out.sort() : out
23
34
 
35
+ out = flags.oval ? out.map((n: any) =>
36
+ (null != n && 'object' === typeof n) ? n : { val$: n }) : out
24
37
 
25
- type OpStep = (node: Node, ctx$: any, buildctx: any) => void
38
+ out = 'string' === typeof flags.sort ?
39
+ out.sort((a: any, b: any) =>
40
+ (a?.[flags.sort as string] < b?.[flags.sort as string] ? -1 :
41
+ a?.[flags.sort as string] > b?.[flags.sort as string] ? 1 : 0)) : out
26
42
 
27
- type OpDef = {
28
- before: OpStep,
29
- after: OpStep,
30
- }
43
+ out = flags.mark ? out
44
+ .map((n: any, i: number, _: any) =>
45
+ (_ = typeof n, (null != n && 'object' === _ ? (n.index$ = i) : null), n)) : out
31
46
 
47
+ if ('function' === typeof apply) {
48
+ out = out.map((n: any, ...args: any[]) =>
49
+ apply(n, ...args))
50
+ }
32
51
 
33
- type Component = (props: any, children?: any) => void
52
+ return out
53
+ }
34
54
 
55
+ const isObject = null != subject && 'object' === typeof subject
35
56
 
57
+ if (!isObject) {
58
+ return out
59
+ }
36
60
 
61
+ let entries: any = Object.entries(subject as any)
37
62
 
38
- function each(subject?: any, apply?: any) {
39
- if (null == apply) {
40
- let out = []
41
- if (Array.isArray(subject)) {
42
- for (let fn of subject) {
43
- out.push('function' === typeof fn ? fn() : fn)
44
- }
45
- return out.sort()
46
- }
47
- else if (null == subject || 'object' !== typeof subject) {
48
- return []
49
- }
50
- }
51
- else if (Array.isArray(subject)) {
52
- return subject.map(apply)
63
+ if (flags.call) {
64
+ entries = entries.map((n: any[]) => ((n[1] = 'function' === typeof n[1] ? n[1]() : n[1]), n))
53
65
  }
54
66
 
55
- if (null == subject || 'object' !== typeof subject) {
56
- return []
67
+ if (flags.oval) {
68
+ out = entries.map((n: any[], _: any) =>
69
+ (_ = typeof n[1],
70
+ (null != n[1] && 'object' === _) ? n[1] :
71
+ (n[1] = { key$: n[0], val$: n[1] }), n))
57
72
  }
58
73
 
59
- const entries: any = Object.entries(subject).map((n: any[], _: any) =>
60
- (_ = typeof n[1],
61
- (null != n[1] && 'object' === _) ? (n[1].key$ = n[0]) :
62
- (n[1] = { name: n[0], key$: n[0], val$: n[1] }), n))
74
+ if (flags.mark) {
75
+ entries.map((n: any[], _: any) =>
76
+ (_ = typeof n[1],
77
+ (null != n[1] && 'object' === _) ? (n[1].key$ = n[0]) : n[1], n))
78
+ }
63
79
 
64
- if (1 < entries.length) {
65
- if (entries[0] && entries[0][1] && 'string' === typeof entries[0][1].name) {
80
+ if (1 < entries.length && flags.sort) {
81
+ if (null != entries[0][1] && 'object' === typeof entries[0][1]) {
82
+ let sprop = 'string' === flags.sort ? flags.sort : 'key$'
66
83
  entries.sort((a: any, b: any) =>
67
- a[1].name < b[1].name ? -1 : b[1].name < a[1].name ? 1 : 0)
84
+ a[1]?.[sprop] < b[1]?.[sprop] ? -1 : b[1]?.[sprop] < a[1]?.[sprop] ? 1 : 0)
68
85
  }
69
- else if (entries[0] && entries[0][1] && 'string' === typeof entries[0][1].key$) {
86
+ else {
70
87
  entries.sort((a: any, b: any) =>
71
- a[1].key$ < b[1].key$ ? -1 : b[1].key$ < a[1].key$ ? 1 : 0)
88
+ a[1] < b[1] ? -1 : b[1] < a[1] ? 1 : 0)
72
89
  }
73
90
  }
74
91
 
75
- apply = 'function' === typeof apply ? apply : (x: any) => x
92
+ if ('function' === typeof apply) {
93
+ out = entries.map((n: any[], ...args: any[]) =>
94
+ apply(n[1], n[0], ...args))
95
+ }
96
+ else {
97
+ out = entries.map((n: any[]) => n[1])
98
+ }
76
99
 
77
- return entries.map((n: any, ...args: any[]) =>
78
- apply(n[1], n[0], ...args))
100
+ return out
79
101
  }
80
102
 
81
103
 
@@ -218,8 +240,13 @@ function getx(root: any, path: string | string[]): any {
218
240
  out = each(node)
219
241
  .filter((child: any) => undefined != getx(child, ftokens))
220
242
 
221
- if (null !== node && 'object' === typeof node && !Array.isArray(node)) {
222
- out = out.reduce((a: any, n: any) => (a[n.key$] = n, delete n.key$, a), {})
243
+ if (null != node && 'object' === typeof node) {
244
+ if (Array.isArray(node)) {
245
+ out = out.map((n: any) => (delete n.index$, n))
246
+ }
247
+ else {
248
+ out = out.reduce((a: any, n: any) => (a[n.key$] = n, delete n.key$, a), {})
249
+ }
223
250
  }
224
251
 
225
252
  node = out
@@ -624,14 +651,6 @@ const BINARY_EXT = [
624
651
 
625
652
 
626
653
 
627
- export type {
628
- JostracaOptions,
629
- Node,
630
- OpStep,
631
- OpDef,
632
- Component,
633
- }
634
-
635
654
 
636
655
  export {
637
656
  each,
package/gen/readme.js~ DELETED
@@ -1 +0,0 @@
1
-