react-hook-core 0.5.12 → 0.5.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.
package/lib/com.js CHANGED
@@ -8,7 +8,7 @@ function Error(p) {
8
8
  {},
9
9
  React.createElement(
10
10
  "header",
11
- {},
11
+ { className: "error-header" },
12
12
  p.back && React.createElement("button", { type: "button", id: "backBtn", name: "backBtn", className: "btn-back", onClick: p.back }),
13
13
  React.createElement("h2", {}, p.title),
14
14
  ),
package/lib/core.js CHANGED
@@ -7,11 +7,23 @@ var resources = (function () {
7
7
  resources.getSortId = function (field) {
8
8
  return field + "Sort"
9
9
  }
10
- resources.removePhoneFormat = function (phone) {
11
- return phone ? phone.replace(resources.phone, "") : ""
10
+ resources.normalizePhone = function (s) {
11
+ if (!s) {
12
+ return ""
13
+ }
14
+ var len = s.length
15
+ var buf = new Array(len)
16
+ var j = 0
17
+ for (var i = 0; i < len; i++) {
18
+ var c = s.charCodeAt(i)
19
+ if (c === 43 || (c >= 48 && c <= 57)) {
20
+ buf[j++] = s[i]
21
+ }
22
+ }
23
+ return j === len ? buf.join("") : buf.slice(0, j).join("")
12
24
  }
13
- resources.removeFaxFormat = function (fax) {
14
- return fax ? fax.replace(resources.fax, "") : ""
25
+ resources.normalizeFax = function (fax) {
26
+ return resources.normalizePhone(fax)
15
27
  }
16
28
  resources._cache = {}
17
29
  resources.cache = true
@@ -21,16 +33,14 @@ var resources = (function () {
21
33
  resources.defaultLimit = 24
22
34
  resources.limits = exports.pageSizes
23
35
  resources.pageMaxSize = 7
24
- resources.phone = / |\-|\.|\(|\)/g
25
- resources.fax = / |\-|\.|\(|\)/g
26
36
  return resources
27
37
  })()
28
38
  exports.resources = resources
29
- function removePhoneFormat(phone) {
30
- return resources.removePhoneFormat(phone)
39
+ function normalizePhone(phone) {
40
+ return resources.normalizePhone(phone)
31
41
  }
32
- exports.removePhoneFormat = removePhoneFormat
33
- function removeFaxFormat(fax) {
34
- return resources.removeFaxFormat(fax)
42
+ exports.normalizePhone = normalizePhone
43
+ function normalizeFax(fax) {
44
+ return resources.normalizeFax(fax)
35
45
  }
36
- exports.removeFaxFormat = removeFaxFormat
46
+ exports.normalizeFax = normalizeFax
package/lib/index.js CHANGED
@@ -160,6 +160,13 @@ exports.datetimeToString = datetimeToString
160
160
  function pad(n) {
161
161
  return n < 10 ? "0" + n : n.toString()
162
162
  }
163
+ function numberToString(n) {
164
+ if (n == null || isNaN(n)) {
165
+ return ""
166
+ }
167
+ return n.toString()
168
+ }
169
+ exports.numberToString = numberToString
163
170
  exports.scrollToFocus = function (e, isUseTimeOut) {
164
171
  try {
165
172
  var element = e.target
package/lib/state.js CHANGED
@@ -24,46 +24,86 @@ function getDecimalSeparator(ele) {
24
24
  separator = form.getAttribute("data-decimal-separator")
25
25
  }
26
26
  }
27
- return separator === "," ? "," : "."
27
+ return separator === "," || separator === "٫" ? separator : "."
28
28
  }
29
29
  exports.getDecimalSeparator = getDecimalSeparator
30
+ function normalizeInteger(s) {
31
+ if (!s) {
32
+ return ""
33
+ }
34
+ var len = s.length
35
+ var buf = new Array(len)
36
+ var j = 0
37
+ for (var i = 0; i < len; i++) {
38
+ var c = s.charCodeAt(i)
39
+ if (c >= 48 && c <= 57) {
40
+ buf[j++] = s[i]
41
+ }
42
+ }
43
+ return j === len ? buf.join("") : buf.slice(0, j).join("")
44
+ }
45
+ exports.normalizeInteger = normalizeInteger
30
46
  function removeSeparators(s) {
31
- if (!s) return ""
32
- var buf = []
33
- var idx = 0
34
- for (var i = 0; i < s.length; i++) {
47
+ if (!s) {
48
+ return ""
49
+ }
50
+ var len = s.length
51
+ var buffer = new Uint16Array(len)
52
+ var write = 0
53
+ for (var i = 0; i < len; i++) {
35
54
  var c = s.charCodeAt(i)
36
- if (c === 45 || (c >= 48 && c <= 57)) {
37
- buf[idx++] = s[i]
55
+ if ((c >= 48 && c <= 57) || c === 46) {
56
+ buffer[write++] = c
38
57
  }
39
58
  }
40
- return buf.join("")
59
+ return String.fromCharCode.apply(null, buffer.subarray(0, write))
41
60
  }
42
61
  exports.removeSeparators = removeSeparators
43
- var r1 = / |,|\$|€|£|¥|'|٬|،| /g
44
- var r2 = / |\.|\$|€|£|¥|'|٬|،| /g
62
+ function normalizeNumber(s) {
63
+ if (!s) {
64
+ return ""
65
+ }
66
+ var len = s.length
67
+ var buf = new Array(len)
68
+ var j = 0
69
+ for (var i = 0; i < len; i++) {
70
+ var c = s.charCodeAt(i)
71
+ if (c >= 48 && c <= 57) {
72
+ buf[j++] = s[i]
73
+ } else if (c === 44 || c === 1643) {
74
+ buf[j++] = "."
75
+ }
76
+ }
77
+ return j === len ? buf.join("") : buf.slice(0, j).join("")
78
+ }
79
+ exports.normalizeNumber = normalizeNumber
45
80
  function updateNumber(e, o, setObj, decimalSeparator, callback, formatStr) {
46
81
  var ctrl = e.target
47
82
  var v0 = formatStr ? formatStr(ctrl.value) : ctrl.value
48
83
  var dataField = ctrl.getAttribute("data-field")
49
84
  var field = dataField ? dataField : ctrl.name
50
- var v = decimalSeparator === "," ? v0.replace(r2, "") : v0.replace(r1, "")
51
- if (v.indexOf(",") >= 0) {
52
- v = v.replace(",", ".")
85
+ var v = decimalSeparator === "," || decimalSeparator === "٫" ? normalizeNumber(v0) : removeSeparators(v0)
86
+ if (v === "" || v == null) {
87
+ reflect_1.setValue(o, field, undefined)
88
+ } else {
89
+ var val = isNaN(v) ? undefined : parseFloat(v)
90
+ reflect_1.setValue(o, field, val)
53
91
  }
54
- var val = isNaN(v) ? undefined : parseFloat(v)
55
- reflect_1.setValue(o, field, val)
56
92
  setObj(__assign({}, o))
57
93
  if (callback) {
58
94
  callback()
59
95
  }
60
96
  }
61
97
  exports.updateNumber = updateNumber
62
- function formatAndUpdateState(e, o, setObj, formatStr, callback) {
63
- updateState(e, o, setObj, callback, formatStr)
98
+ function formatAndUpdateState(e, o, setObj, formatStr, decimalSeparator, callback) {
99
+ updateState(e, o, setObj, decimalSeparator, formatStr, callback)
64
100
  }
65
101
  exports.formatAndUpdateState = formatAndUpdateState
66
- function updateState(e, o, setObj, callback, formatStr) {
102
+ function updateStateAndCallback(e, o, setObj, callback, decimalSeparator, formatStr) {
103
+ updateState(e, o, setObj, decimalSeparator, formatStr, callback)
104
+ }
105
+ exports.updateStateAndCallback = updateStateAndCallback
106
+ function updateState(e, o, setObj, decimalSeparator, formatStr, callback) {
67
107
  var ctrl = e.target
68
108
  var v0 = formatStr ? formatStr(ctrl.value) : ctrl.value
69
109
  var dataField = ctrl.getAttribute("data-field")
@@ -117,23 +157,28 @@ function updateState(e, o, setObj, callback, formatStr) {
117
157
  } else {
118
158
  var datatype = ctrl.getAttribute("data-type")
119
159
  if (datatype === "phone") {
120
- var val = core_1.removePhoneFormat(v0)
160
+ var val = core_1.normalizePhone(v0)
121
161
  reflect_1.setValue(o, field, val)
122
162
  } else if (datatype === "fax") {
123
- var val = core_1.removeFaxFormat(v0)
163
+ var val = core_1.normalizeFax(v0)
124
164
  reflect_1.setValue(o, field, val)
125
165
  } else if (datatype === "integer") {
126
- var v = removeSeparators(v0)
127
- var val = isNaN(v) ? undefined : parseFloat(v)
128
- reflect_1.setValue(o, field, val)
166
+ var v = normalizeInteger(v0)
167
+ if (v === "" || v == null) {
168
+ reflect_1.setValue(o, field, undefined)
169
+ } else {
170
+ var val = isNaN(v) ? undefined : parseFloat(v)
171
+ reflect_1.setValue(o, field, val)
172
+ }
129
173
  } else if (datatype === "number") {
130
- var decimalSeparator = getDecimalSeparator(ctrl)
131
- var v = decimalSeparator === "," ? v0.replace(r2, "") : v0.replace(r1, "")
132
- if (v.indexOf(",") >= 0) {
133
- v = v.replace(",", ".")
174
+ var sep = decimalSeparator ? decimalSeparator : getDecimalSeparator(ctrl)
175
+ var v = sep === "," || sep === "٫" ? normalizeNumber(v0) : removeSeparators(v0)
176
+ if (v === "" || v == null) {
177
+ reflect_1.setValue(o, field, undefined)
178
+ } else {
179
+ var val = isNaN(v) ? undefined : parseFloat(v)
180
+ reflect_1.setValue(o, field, val)
134
181
  }
135
- var val = isNaN(v) ? undefined : parseFloat(v)
136
- reflect_1.setValue(o, field, val)
137
182
  } else {
138
183
  reflect_1.setValue(o, field, v0)
139
184
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-hook-core",
3
- "version": "0.5.12",
3
+ "version": "0.5.15",
4
4
  "description": "react",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
package/src/com.ts CHANGED
@@ -12,7 +12,7 @@ export function Error(p: ErrorProps) {
12
12
  {},
13
13
  React.createElement(
14
14
  "header",
15
- {},
15
+ { className: "error-header" },
16
16
  p.back && React.createElement("button", { type: "button", id: "backBtn", name: "backBtn", className: "btn-back", onClick: p.back }),
17
17
  React.createElement("h2", {}, p.title),
18
18
  ),
@@ -20,7 +20,7 @@ export function Error(p: ErrorProps) {
20
20
  )
21
21
  /*
22
22
  <div>
23
- <header>
23
+ <header className="error-header">
24
24
  <button type="button" id="backBtn" name="backBtn" className="btn-back" onClick={() => navigate(-1)}></button>
25
25
  <h2>{resource.error_404_title}</h2>
26
26
  </header>
package/src/core.ts CHANGED
@@ -14,23 +14,33 @@ export class resources {
14
14
  static defaultLimit = 24
15
15
  static limits = pageSizes
16
16
  static pageMaxSize = 7
17
- static phone = / |\-|\.|\(|\)/g
18
- static fax = / |\-|\.|\(|\)/g
19
17
  static getSortId(field: string): string {
20
18
  return field + "Sort"
21
19
  }
22
- static removePhoneFormat(phone?: string | null): string {
23
- return phone ? phone.replace(resources.phone, "") : ""
20
+ static normalizePhone(s?: string | null): string {
21
+ if (!s) {
22
+ return ""
23
+ }
24
+ const len = s.length
25
+ const buf = new Array<string>(len)
26
+ let j = 0
27
+ for (let i = 0; i < len; i++) {
28
+ const c = s.charCodeAt(i)
29
+ if (c === 43 || (c >= 48 && c <= 57)) {
30
+ buf[j++] = s[i]
31
+ }
32
+ }
33
+ return j === len ? buf.join("") : buf.slice(0, j).join("")
24
34
  }
25
- static removeFaxFormat(fax?: string | null): string {
26
- return fax ? fax.replace(resources.fax, "") : ""
35
+ static normalizeFax(fax?: string | null): string {
36
+ return resources.normalizePhone(fax)
27
37
  }
28
38
  }
29
- export function removePhoneFormat(phone?: string | null): string {
30
- return resources.removePhoneFormat(phone)
39
+ export function normalizePhone(phone?: string | null): string {
40
+ return resources.normalizePhone(phone)
31
41
  }
32
- export function removeFaxFormat(fax?: string | null): string {
33
- return resources.removeFaxFormat(fax)
42
+ export function normalizeFax(fax?: string | null): string {
43
+ return resources.normalizeFax(fax)
34
44
  }
35
45
 
36
46
  export interface StringMap {
package/src/index.ts CHANGED
@@ -211,6 +211,12 @@ export function datetimeToString(date?: Date | string): string | undefined {
211
211
  function pad(n: number): string {
212
212
  return n < 10 ? "0" + n : n.toString()
213
213
  }
214
+ export function numberToString(n?: number | null) {
215
+ if (n == null || isNaN(n)) {
216
+ return ""
217
+ }
218
+ return n.toString()
219
+ }
214
220
 
215
221
  export const scrollToFocus = (e: any, isUseTimeOut?: boolean) => {
216
222
  try {
package/src/state.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { ChangeEvent } from "react"
2
- import { removeFaxFormat, removePhoneFormat } from "./core"
2
+ import { normalizeFax, normalizePhone } from "./core"
3
3
  import { setValue } from "./reflect"
4
4
 
5
5
  export function getDecimalSeparator(ele: HTMLInputElement): string {
@@ -10,25 +10,62 @@ export function getDecimalSeparator(ele: HTMLInputElement): string {
10
10
  separator = form.getAttribute("data-decimal-separator")
11
11
  }
12
12
  }
13
- return separator === "," ? "," : "."
13
+ return separator === "," || separator === "٫" ? separator : "."
14
14
  }
15
-
16
- export function removeSeparators(s?: string | null): string {
17
- if (!s) return ""
18
- const buf: string[] = []
19
- let idx = 0
20
- for (let i = 0; i < s.length; i++) {
15
+ export function normalizeInteger(s?: string | null): string {
16
+ if (!s) {
17
+ return ""
18
+ }
19
+ const len = s.length
20
+ const buf = new Array<string>(len)
21
+ let j = 0
22
+ for (let i = 0; i < len; i++) {
21
23
  const c = s.charCodeAt(i)
24
+ if (c >= 48 && c <= 57) {
25
+ buf[j++] = s[i]
26
+ }
27
+ }
28
+ return j === len ? buf.join("") : buf.slice(0, j).join("")
29
+ }
30
+ export function removeSeparators(s?: string | null): string {
31
+ if (!s) {
32
+ return ""
33
+ }
34
+ const len = s.length
35
+ const buffer = new Uint16Array(len) // preallocate max possible
36
+ let write = 0
22
37
 
23
- if (c === 45 || (c >= 48 && c <= 57)) {
24
- buf[idx++] = s[i]
38
+ for (let i = 0; i < len; i++) {
39
+ const c = s.charCodeAt(i)
40
+ // '0'–'9' (48–57), '.' (46)
41
+ if ((c >= 48 && c <= 57) || c === 46) {
42
+ buffer[write++] = c
25
43
  }
26
44
  }
27
- return buf.join("")
45
+ // Convert only the used portion to string
46
+ return String.fromCharCode.apply(null, buffer.subarray(0, write) as any)
28
47
  }
48
+ // Keep digits 0–9 ; Replace , and ٫ (Arabic decimal separator) → . ; Remove everything else
49
+ export function normalizeNumber(s?: string | null): string {
50
+ if (!s) {
51
+ return ""
52
+ }
53
+ const len = s.length
54
+ const buf = new Array<string>(len)
55
+ let j = 0
56
+ for (let i = 0; i < len; i++) {
57
+ const c = s.charCodeAt(i)
29
58
 
30
- const r1 = / |,|\$|€|£|¥|'|٬|،| /g
31
- const r2 = / |\.|\$|€|£|¥|'|٬|،| /g
59
+ if (c >= 48 && c <= 57) {
60
+ buf[j++] = s[i]
61
+ } else if (c === 44 || c === 1643) {
62
+ buf[j++] = "."
63
+ }
64
+ }
65
+ return j === len ? buf.join("") : buf.slice(0, j).join("")
66
+ }
67
+ // const r1 = / |,|\$|€|£|¥|'|٬|،| /g
68
+ // const r2 = / |\.|\$|€|£|¥|'|٬|،| /g
32
69
  export function updateNumber<T>(
33
70
  e: ChangeEvent<HTMLInputElement>,
34
71
  o: T,
@@ -41,12 +78,13 @@ export function updateNumber<T>(
41
78
  const v0: string = formatStr ? formatStr(ctrl.value) : ctrl.value
42
79
  const dataField = ctrl.getAttribute("data-field")
43
80
  const field = dataField ? dataField : ctrl.name
44
- let v = decimalSeparator === "," ? v0.replace(r2, "") : v0.replace(r1, "")
45
- if (v.indexOf(",") >= 0) {
46
- v = v.replace(",", ".")
81
+ let v = decimalSeparator === "," || decimalSeparator === "٫" ? normalizeNumber(v0) : removeSeparators(v0)
82
+ if (v === "" || v == null) {
83
+ setValue(o, field, undefined)
84
+ } else {
85
+ const val = isNaN(v as any) ? undefined : parseFloat(v)
86
+ setValue(o, field, val)
47
87
  }
48
- const val = isNaN(v as any) ? undefined : parseFloat(v)
49
- setValue(o, field, val)
50
88
  setObj({ ...o })
51
89
  if (callback) {
52
90
  callback()
@@ -61,11 +99,12 @@ export function formatAndUpdateState<T>(
61
99
  o: T,
62
100
  setObj: (v: React.SetStateAction<T>) => void,
63
101
  formatStr?: (s?: string) => string,
102
+ decimalSeparator?: string,
64
103
  callback?: () => void,
65
104
  ) {
66
- updateState(e, o, setObj, callback, formatStr)
105
+ updateState(e, o, setObj, decimalSeparator, formatStr, callback)
67
106
  }
68
- export function updateState<T>(
107
+ export function updateStateAndCallback<T>(
69
108
  e:
70
109
  | ChangeEvent<HTMLInputElement>
71
110
  | ChangeEvent<HTMLSelectElement>
@@ -74,7 +113,22 @@ export function updateState<T>(
74
113
  o: T,
75
114
  setObj: (v: React.SetStateAction<T>) => void,
76
115
  callback?: () => void,
116
+ decimalSeparator?: string,
77
117
  formatStr?: (s?: string) => string,
118
+ ) {
119
+ updateState(e, o, setObj, decimalSeparator, formatStr, callback)
120
+ }
121
+ export function updateState<T>(
122
+ e:
123
+ | ChangeEvent<HTMLInputElement>
124
+ | ChangeEvent<HTMLSelectElement>
125
+ | ChangeEvent<HTMLTextAreaElement>
126
+ | ChangeEvent<HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement>,
127
+ o: T,
128
+ setObj: (v: React.SetStateAction<T>) => void,
129
+ decimalSeparator?: string,
130
+ formatStr?: (s?: string) => string,
131
+ callback?: () => void,
78
132
  ) {
79
133
  const ctrl = e.target
80
134
  const v0: string = formatStr ? formatStr(ctrl.value) : ctrl.value
@@ -125,23 +179,28 @@ export function updateState<T>(
125
179
  } else {
126
180
  const datatype = ctrl.getAttribute("data-type")
127
181
  if (datatype === "phone") {
128
- const val = removePhoneFormat(v0)
182
+ const val = normalizePhone(v0)
129
183
  setValue(o, field, val)
130
184
  } else if (datatype === "fax") {
131
- const val = removeFaxFormat(v0)
185
+ const val = normalizeFax(v0)
132
186
  setValue(o, field, val)
133
187
  } else if (datatype === "integer") {
134
- let v = removeSeparators(v0)
135
- const val = isNaN(v as any) ? undefined : parseFloat(v)
136
- setValue(o, field, val)
188
+ let v = normalizeInteger(v0)
189
+ if (v === "" || v == null) {
190
+ setValue(o, field, undefined)
191
+ } else {
192
+ const val = isNaN(v as any) ? undefined : parseFloat(v)
193
+ setValue(o, field, val)
194
+ }
137
195
  } else if (datatype === "number") {
138
- const decimalSeparator = getDecimalSeparator(ctrl as HTMLInputElement)
139
- let v = decimalSeparator === "," ? v0.replace(r2, "") : v0.replace(r1, "")
140
- if (v.indexOf(",") >= 0) {
141
- v = v.replace(",", ".")
196
+ const sep = decimalSeparator ? decimalSeparator : getDecimalSeparator(ctrl as HTMLInputElement)
197
+ let v = sep === "," || sep === "٫" ? normalizeNumber(v0) : removeSeparators(v0)
198
+ if (v === "" || v == null) {
199
+ setValue(o, field, undefined)
200
+ } else {
201
+ const val = isNaN(v as any) ? undefined : parseFloat(v)
202
+ setValue(o, field, val)
142
203
  }
143
- const val = isNaN(v as any) ? undefined : parseFloat(v)
144
- setValue(o, field, val)
145
204
  } else {
146
205
  setValue(o, field, v0)
147
206
  }