react-app-utilities 0.0.0-stage → 0.0.1

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.
package/src/reflect.ts ADDED
@@ -0,0 +1,244 @@
1
+ export function clone(obj: any): any {
2
+ if (!obj) {
3
+ return obj
4
+ }
5
+ if (obj instanceof Date) {
6
+ return new Date(obj.getTime())
7
+ }
8
+ if (typeof obj !== "object") {
9
+ return obj
10
+ }
11
+ if (Array.isArray(obj)) {
12
+ const arr = []
13
+ for (const sub of obj) {
14
+ const c = clone(sub)
15
+ arr.push(c)
16
+ }
17
+ return arr
18
+ }
19
+ const x: any = {}
20
+ const keys = Object.keys(obj)
21
+ for (const k of keys) {
22
+ const v = obj[k]
23
+ if (v instanceof Date) {
24
+ x[k] = new Date(v.getTime())
25
+ } else {
26
+ switch (typeof v) {
27
+ case "object":
28
+ x[k] = clone(v)
29
+ break
30
+ default:
31
+ x[k] = v
32
+ break
33
+ }
34
+ }
35
+ }
36
+ return x
37
+ }
38
+
39
+ export function diff(obj1: any, obj2: any): string[] {
40
+ const fields = []
41
+ const key1s = Object.keys(obj2)
42
+ for (const k of key1s) {
43
+ const v1 = obj2[k]
44
+ const v2 = obj1[k]
45
+ if (v1 == null) {
46
+ if (v2 != null) {
47
+ fields.push(k)
48
+ }
49
+ } else {
50
+ if (typeof v1 !== "object") {
51
+ if (v1 !== v2) {
52
+ fields.push(k)
53
+ }
54
+ } else {
55
+ const e = equal(v1, v2)
56
+ if (!e) {
57
+ fields.push(k)
58
+ }
59
+ }
60
+ }
61
+ }
62
+ const key2s = Object.keys(obj1)
63
+ const ni = notIn(key1s, key2s)
64
+ for (const n of ni) {
65
+ fields.push(n)
66
+ }
67
+ return fields
68
+ }
69
+ export function notIn(s1: string[], s2: string[]): string[] {
70
+ const r = []
71
+ for (const s of s2) {
72
+ if (s1.indexOf(s) < 0) {
73
+ r.push(s)
74
+ }
75
+ }
76
+ return r
77
+ }
78
+
79
+ export function makeDiff<T>(o1: T, o2?: T, keys?: string[], version?: string): Partial<T> {
80
+ if (o1 && !o2) {
81
+ return o1
82
+ }
83
+ const obj1: any = o1
84
+ const obj2: any = o2
85
+ const obj3: any = {}
86
+ const s = diff(obj1, obj2)
87
+ if (s.length === 0) {
88
+ return obj3
89
+ }
90
+ for (const d of s) {
91
+ obj3[d] = obj1[d]
92
+ }
93
+ if (keys && keys.length > 0) {
94
+ for (const x of keys) {
95
+ if (x.length > 0) {
96
+ obj3[x] = obj2[x]
97
+ }
98
+ }
99
+ }
100
+ if (version && version.length > 0) {
101
+ obj3[version] = obj2[version]
102
+ }
103
+ return obj3
104
+ }
105
+ export function hasDiff<T>(o1: T, o2?: T, keys?: string[], version?: string): boolean {
106
+ if (o1 && !o2) {
107
+ return true
108
+ }
109
+ const diff = makeDiff(o1, o2, keys, version)
110
+ return !isEmpty(diff)
111
+ }
112
+ export function isEmpty(obj: object): boolean {
113
+ for (const _ in obj) {
114
+ if (Object.prototype.hasOwnProperty.call(obj, _)) {
115
+ return false
116
+ }
117
+ }
118
+ return true
119
+ }
120
+
121
+ export function equal(obj1: any, obj2: any): boolean {
122
+ if (obj1 == null && obj2 == null) {
123
+ return true
124
+ }
125
+ if (obj1 == null || obj2 == null) {
126
+ return false
127
+ }
128
+ if (typeof obj1 !== typeof obj2) {
129
+ return false
130
+ }
131
+ if (typeof obj1 !== "object") {
132
+ return obj1 === obj2
133
+ }
134
+ if (obj1 instanceof Date) {
135
+ if (!(obj2 instanceof Date)) {
136
+ return false
137
+ }
138
+ return obj1.getTime() === obj2.getTime()
139
+ }
140
+ if ((Array.isArray(obj1) && !Array.isArray(obj2)) || (!Array.isArray(obj1) && Array.isArray(obj2))) {
141
+ return false
142
+ }
143
+ if (!Array.isArray(obj1) && !Array.isArray(obj2)) {
144
+ const key1s = Object.keys(obj1)
145
+ const key2s = Object.keys(obj2)
146
+ if (key1s.length !== key2s.length) {
147
+ return false
148
+ }
149
+ for (const k of key1s) {
150
+ const v = obj1[k]
151
+ if (typeof v !== "object") {
152
+ if (v !== obj2[k]) {
153
+ return false
154
+ }
155
+ } else {
156
+ const e = equal(v, obj2[k])
157
+ if (e === false) {
158
+ return false
159
+ }
160
+ }
161
+ }
162
+ return true
163
+ }
164
+ return equalArrays(obj1, obj2)
165
+ }
166
+ export function equalArrays<T>(ar1: T[], ar2: T[]): boolean {
167
+ if (ar1 == null && ar2 == null) {
168
+ return true
169
+ }
170
+ if (ar1 == null || ar2 == null) {
171
+ return false
172
+ }
173
+ if (ar1.length !== ar2.length) {
174
+ return false
175
+ }
176
+ for (let i = 0; i < ar1.length; i++) {
177
+ const e = equal(ar1[i], ar2[i])
178
+ if (e === false) {
179
+ return false
180
+ }
181
+ }
182
+ return true
183
+ }
184
+
185
+ export function getArray<T>(list: T[] | undefined | null, name: string, v: boolean | string | number): T[] {
186
+ const arrs = []
187
+ if (list) {
188
+ for (const obj of list) {
189
+ if ((obj as any)[name] === v) {
190
+ arrs.push(obj)
191
+ }
192
+ }
193
+ }
194
+ return arrs
195
+ }
196
+ export function getDiffArray<T>(list: T[] | undefined | null, name: string, v: boolean | string | number): T[] {
197
+ const arrs = []
198
+ if (list) {
199
+ for (const obj of list) {
200
+ if ((obj as any)[name] !== v) {
201
+ arrs.push(obj)
202
+ }
203
+ }
204
+ }
205
+ return arrs
206
+ }
207
+
208
+ export function getDirectValue(obj: any, key: string): any {
209
+ if (obj && obj.hasOwnProperty(key)) {
210
+ return obj[key]
211
+ }
212
+ return null
213
+ }
214
+
215
+ export function setValue(obj: any, key: string, value: any): any {
216
+ let replaceKey = key.replace(/\[/g, ".[").replace(/\.\./g, ".")
217
+ if (replaceKey.indexOf(".") === 0) {
218
+ replaceKey = replaceKey.slice(1, replaceKey.length)
219
+ }
220
+ const keys = replaceKey.split(".")
221
+ const firstKey = keys.shift()
222
+ if (!firstKey) {
223
+ return
224
+ }
225
+ const isArrayKey = /\[([0-9]+)\]/.test(firstKey)
226
+ if (keys.length > 0) {
227
+ const firstKeyValue = obj[firstKey] || {}
228
+ const returnValue = setValue(firstKeyValue, keys.join("."), value)
229
+ return setKey(obj, isArrayKey, firstKey, returnValue)
230
+ }
231
+ return setKey(obj, isArrayKey, firstKey, value)
232
+ }
233
+ const setKey = (_object: any, _isArrayKey: boolean, _key: string, _nextValue: any) => {
234
+ if (_isArrayKey) {
235
+ if (_object.length > _key) {
236
+ _object[_key] = _nextValue
237
+ } else {
238
+ _object.push(_nextValue)
239
+ }
240
+ } else {
241
+ _object[_key] = _nextValue
242
+ }
243
+ return _object
244
+ }
package/src/route.ts ADDED
@@ -0,0 +1,152 @@
1
+ import queryString from "query-string"
2
+ import * as React from "react"
3
+ import { NavigateFunction } from "react-router"
4
+ import { StringMap } from "./core"
5
+ import { hasDiff } from "./reflect"
6
+ import { Filter } from "./search"
7
+
8
+ export function onBack<T>(
9
+ e: React.MouseEvent<HTMLElement, MouseEvent>,
10
+ navigate: NavigateFunction,
11
+ confirm: (msg: string, yesCallback?: () => void) => void,
12
+ resource: StringMap,
13
+ o1: T,
14
+ o2?: T,
15
+ keys?: string[],
16
+ version?: string,
17
+ ) {
18
+ e.preventDefault()
19
+ goBack(navigate, confirm, resource, o1, o2, keys, version)
20
+ }
21
+
22
+ export function goBack<T>(
23
+ navigate: NavigateFunction,
24
+ confirm: (msg: string, yesCallback?: () => void) => void,
25
+ resource: StringMap,
26
+ o1: T,
27
+ o2?: T,
28
+ keys?: string[],
29
+ version?: string,
30
+ ) {
31
+ if (!o2) {
32
+ navigate(-1)
33
+ } else if (!hasDiff(o1, o2, keys, version)) {
34
+ navigate(-1)
35
+ } else {
36
+ confirm(resource.msg_confirm_back, () => navigate(-1))
37
+ }
38
+ }
39
+
40
+ export function buildFromUrl<T extends Filter>(model?: T): T {
41
+ return buildParameters<T>(window.location.search, model)
42
+ }
43
+
44
+ export function buildParameters<T>(url: string, model?: T): T {
45
+ var query = url
46
+ var index = url.indexOf("?")
47
+
48
+ if (index >= 0) {
49
+ query = url.substring(index + 1)
50
+ }
51
+
52
+ var parsed = queryString.parse(query, {
53
+ parseNumbers: true,
54
+ parseBooleans: true,
55
+ })
56
+
57
+ return convertToObject(parsed as any, model)
58
+ }
59
+
60
+ export function convertToObject<T>(input: { [key: string]: any }, model?: T): T {
61
+ if (model) {
62
+ return mapToModel(input, model)
63
+ }
64
+
65
+ var output: any = {}
66
+
67
+ for (var key in input) {
68
+ if (!Object.prototype.hasOwnProperty.call(input, key)) {
69
+ continue
70
+ }
71
+
72
+ var value = input[key]
73
+ var keys = key.split(".")
74
+ var current = output
75
+
76
+ for (var i = 0; i < keys.length; i++) {
77
+ var k = keys[i]
78
+
79
+ if (i === keys.length - 1) {
80
+ current[k] = parseValue(value)
81
+ } else {
82
+ if (!current[k]) {
83
+ current[k] = {}
84
+ }
85
+ current = current[k]
86
+ }
87
+ }
88
+ }
89
+
90
+ return output as T
91
+ }
92
+
93
+ function mapToModel<T>(input: { [key: string]: any }, model: T): T {
94
+ var result: any = {}
95
+ var key: string
96
+
97
+ for (key in model as any) {
98
+ if (!Object.prototype.hasOwnProperty.call(model, key)) {
99
+ continue
100
+ }
101
+
102
+ var modelValue = (model as any)[key]
103
+ var inputValue = input[key]
104
+
105
+ if (inputValue === undefined) {
106
+ result[key] = modelValue
107
+ continue
108
+ }
109
+
110
+ if (modelValue instanceof Date) {
111
+ result[key] = new Date(inputValue)
112
+ } else if (typeof modelValue === "number") {
113
+ result[key] = Number(inputValue)
114
+ } else if (typeof modelValue === "boolean") {
115
+ result[key] = Boolean(inputValue)
116
+ } else if (typeof modelValue === "string") {
117
+ result[key] = String(inputValue)
118
+ } else if (Object.prototype.toString.call(modelValue) === "[object Array]") {
119
+ if (Object.prototype.toString.call(inputValue) === "[object Array]") {
120
+ result[key] = inputValue
121
+ } else {
122
+ result[key] = [inputValue]
123
+ }
124
+ } else if (typeof modelValue === "object" && modelValue !== null) {
125
+ result[key] = mapToModel(inputValue || {}, modelValue)
126
+ } else {
127
+ result[key] = inputValue
128
+ }
129
+ }
130
+
131
+ return result
132
+ }
133
+
134
+ function parseValue(value: any): any {
135
+ if (typeof value !== "string") {
136
+ return value
137
+ }
138
+
139
+ if (!isNaN(Number(value))) {
140
+ return Number(value)
141
+ }
142
+
143
+ if (value === "true") {
144
+ return true
145
+ }
146
+
147
+ if (value === "false") {
148
+ return false
149
+ }
150
+
151
+ return value
152
+ }