react-hook-core 0.4.5 → 0.4.7
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 +33 -11
- package/lib/core.js +103 -157
- package/lib/diff.js +28 -30
- package/lib/edit.js +54 -112
- package/lib/error.js +53 -0
- package/lib/formutil.js +58 -72
- package/lib/index.js +110 -95
- package/lib/input.js +31 -32
- package/lib/merge.js +26 -16
- package/lib/reflect.js +140 -147
- package/lib/route.js +45 -53
- package/lib/search.js +233 -475
- package/lib/state.js +159 -168
- package/lib/update.js +69 -64
- package/lib/useEdit.js +421 -348
- package/lib/useMessage.js +20 -20
- package/lib/useSearch.js +437 -246
- package/lib/useView.js +127 -120
- package/lib/util.js +74 -85
- package/package.json +1 -1
- package/src/com.ts +44 -30
- package/src/core.ts +147 -321
- package/src/diff.ts +54 -24
- package/src/edit.ts +42 -121
- package/src/error.ts +55 -0
- package/src/formutil.ts +51 -62
- package/src/index.ts +129 -114
- package/src/input.ts +44 -38
- package/src/merge.ts +16 -13
- package/src/reflect.ts +100 -101
- package/src/route.ts +40 -40
- package/src/search.ts +130 -316
- package/src/state.ts +135 -124
- package/src/update.ts +41 -41
- package/src/useEdit.ts +450 -319
- package/src/useMessage.ts +21 -21
- package/src/useSearch.ts +493 -277
- package/src/useView.ts +122 -97
- package/src/util.ts +67 -66
package/src/useView.ts
CHANGED
|
@@ -1,146 +1,171 @@
|
|
|
1
|
-
import {useEffect, useState} from
|
|
2
|
-
import {useNavigate, useParams} from
|
|
3
|
-
import {buildId, getModelName as getModelName2, hideLoading, initForm, LoadingService, Locale,
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
1
|
+
import { useEffect, useState } from "react"
|
|
2
|
+
import { useNavigate, useParams } from "react-router"
|
|
3
|
+
import { Attributes, buildId, getModelName as getModelName2, hideLoading, initForm, LoadingService, Locale, ResourceService, showLoading } from "./core"
|
|
4
|
+
import { message, messageByHttpStatus } from "./error"
|
|
5
|
+
import { setReadOnly } from "./formutil"
|
|
6
|
+
import { DispatchWithCallback, useMergeState } from "./merge"
|
|
7
|
+
|
|
8
|
+
export interface ViewParameter {
|
|
9
|
+
resource: ResourceService
|
|
10
|
+
showError: (m: string, header?: string, detail?: string, callback?: () => void) => void
|
|
11
|
+
getLocale?: (profile?: string) => Locale
|
|
12
|
+
loading?: LoadingService
|
|
13
|
+
}
|
|
14
|
+
export interface ViewService<T, ID> {
|
|
15
|
+
metadata?(): Attributes | undefined
|
|
16
|
+
keys?(): string[]
|
|
17
|
+
load(id: ID, ctx?: any): Promise<T | null>
|
|
18
|
+
}
|
|
6
19
|
|
|
7
20
|
export interface BaseViewComponentParam<T, ID> {
|
|
8
|
-
name?: string
|
|
9
|
-
handleNotFound?: (form?: HTMLFormElement) => void
|
|
10
|
-
getModelName?: (f?: HTMLFormElement) => string
|
|
11
|
-
showModel?: (m: T) => void
|
|
12
|
-
load?: (i: ID, callback?: (m: T, showM: (m2: T) => void) => void) => void
|
|
21
|
+
name?: string
|
|
22
|
+
handleNotFound?: (form?: HTMLFormElement) => void
|
|
23
|
+
getModelName?: (f?: HTMLFormElement) => string
|
|
24
|
+
showModel?: (m: T) => void
|
|
25
|
+
load?: (i: ID, callback?: (m: T, showM: (m2: T) => void) => void) => void
|
|
13
26
|
}
|
|
14
27
|
export interface HookBaseViewParameter<T, ID, S> extends BaseViewComponentParam<T, ID> {
|
|
15
|
-
refForm: any
|
|
16
|
-
initialState: S
|
|
17
|
-
service: ((id: ID, ctx?: any) => Promise<T>)|ViewService<T, ID
|
|
18
|
-
resource: ResourceService
|
|
19
|
-
showError: (m: string, header?: string, detail?: string, callback?: () => void) => void
|
|
20
|
-
getLocale?: () => Locale
|
|
21
|
-
loading?: LoadingService
|
|
28
|
+
refForm: any
|
|
29
|
+
initialState: S
|
|
30
|
+
service: ((id: ID, ctx?: any) => Promise<T>) | ViewService<T, ID>
|
|
31
|
+
resource: ResourceService
|
|
32
|
+
showError: (m: string, header?: string, detail?: string, callback?: () => void) => void
|
|
33
|
+
getLocale?: () => Locale
|
|
34
|
+
loading?: LoadingService
|
|
22
35
|
}
|
|
23
36
|
export interface ViewComponentParam<T, ID, S> extends BaseViewComponentParam<T, ID> {
|
|
24
|
-
keys?: string[]
|
|
25
|
-
initialize?: (
|
|
26
|
-
|
|
37
|
+
keys?: string[]
|
|
38
|
+
initialize?: (
|
|
39
|
+
id: ID,
|
|
40
|
+
ld: (i: ID, cb?: (m: T, showF: (model: T) => void) => void) => void,
|
|
41
|
+
setState2: DispatchWithCallback<Partial<S>>,
|
|
42
|
+
callback?: (m: T, showF: (model: T) => void) => void,
|
|
43
|
+
) => void
|
|
44
|
+
callback?: (m: T, showF: (model: T) => void) => void
|
|
27
45
|
}
|
|
28
46
|
export interface HookPropsViewParameter<T, ID, S, P> extends HookPropsBaseViewParameter<T, ID, S, P> {
|
|
29
|
-
keys?: string[]
|
|
30
|
-
initialize?: (
|
|
31
|
-
|
|
47
|
+
keys?: string[]
|
|
48
|
+
initialize?: (
|
|
49
|
+
id: ID,
|
|
50
|
+
ld: (i: ID, cb?: (m: T, showF: (model: T) => void) => void) => void,
|
|
51
|
+
setState2: DispatchWithCallback<Partial<S>>,
|
|
52
|
+
callback?: (m: T, showF: (model: T) => void) => void,
|
|
53
|
+
) => void
|
|
54
|
+
callback?: (m: T, showF: (model: T) => void) => void
|
|
32
55
|
}
|
|
33
56
|
export interface HookPropsBaseViewParameter<T, ID, S, P> extends HookBaseViewParameter<T, ID, S> {
|
|
34
|
-
props: P
|
|
57
|
+
props: P
|
|
35
58
|
}
|
|
36
59
|
export const useViewOne = <T, ID, S>(p: HookBaseViewParameter<T, ID, S>) => {
|
|
37
|
-
return useCoreView(p.refForm, p.initialState, p.service, p, p)
|
|
38
|
-
}
|
|
60
|
+
return useCoreView(p.refForm, p.initialState, p.service, p, p)
|
|
61
|
+
}
|
|
39
62
|
export const useView = <T, ID, S>(
|
|
40
63
|
refForm: any,
|
|
41
64
|
initialState: S,
|
|
42
|
-
service: ((id: ID, ctx?: any) => Promise<T>)|ViewService<T, ID>,
|
|
65
|
+
service: ((id: ID, ctx?: any) => Promise<T>) | ViewService<T, ID>,
|
|
43
66
|
p1: ViewParameter,
|
|
44
|
-
p?: ViewComponentParam<T, ID, S
|
|
45
|
-
|
|
46
|
-
const baseProps = useCoreView(refForm, initialState, service, p1, p)
|
|
47
|
-
const [, setState] = useMergeState<S>(initialState)
|
|
48
|
-
const params = useParams()
|
|
67
|
+
p?: ViewComponentParam<T, ID, S>,
|
|
68
|
+
) => {
|
|
69
|
+
const baseProps = useCoreView(refForm, initialState, service, p1, p)
|
|
70
|
+
const [, setState] = useMergeState<S>(initialState)
|
|
71
|
+
const params = useParams()
|
|
49
72
|
useEffect(() => {
|
|
50
73
|
if (baseProps.refForm) {
|
|
51
|
-
initForm(baseProps.refForm.current)
|
|
74
|
+
initForm(baseProps.refForm.current)
|
|
52
75
|
}
|
|
53
|
-
const id = buildId<ID>(params, p ? p.keys : undefined)
|
|
76
|
+
const id = buildId<ID>(params, p ? p.keys : undefined)
|
|
54
77
|
if (id) {
|
|
55
78
|
if (p && p.initialize) {
|
|
56
|
-
p.initialize(id, baseProps.load, setState, p.callback)
|
|
79
|
+
p.initialize(id, baseProps.load, setState, p.callback)
|
|
57
80
|
} else {
|
|
58
|
-
baseProps.load(id, p ? p.callback : undefined)
|
|
81
|
+
baseProps.load(id, p ? p.callback : undefined)
|
|
59
82
|
}
|
|
60
83
|
}
|
|
61
|
-
}, [])
|
|
62
|
-
return {...baseProps}
|
|
63
|
-
}
|
|
84
|
+
}, [])
|
|
85
|
+
return { ...baseProps }
|
|
86
|
+
}
|
|
64
87
|
|
|
65
88
|
export const useCoreView = <T, ID, S>(
|
|
66
89
|
refForm: any,
|
|
67
90
|
initialState: S,
|
|
68
|
-
service: ((id: ID, ctx?: any) => Promise<T>)|ViewService<T, ID>,
|
|
91
|
+
service: ((id: ID, ctx?: any) => Promise<T>) | ViewService<T, ID>,
|
|
69
92
|
p1: ViewParameter,
|
|
70
|
-
p?: ViewComponentParam<T, ID, S
|
|
71
|
-
|
|
72
|
-
const [state, setState] = useMergeState<S>(initialState)
|
|
73
|
-
const [running, setRunning] = useState<boolean>()
|
|
74
|
-
const navigate = useNavigate()
|
|
93
|
+
p?: ViewComponentParam<T, ID, S>,
|
|
94
|
+
) => {
|
|
95
|
+
const [state, setState] = useMergeState<S>(initialState)
|
|
96
|
+
const [running, setRunning] = useState<boolean>()
|
|
97
|
+
const navigate = useNavigate()
|
|
75
98
|
|
|
76
99
|
const back = (event: any) => {
|
|
77
100
|
if (event) {
|
|
78
|
-
event.preventDefault()
|
|
101
|
+
event.preventDefault()
|
|
79
102
|
}
|
|
80
|
-
navigate(-1)
|
|
81
|
-
}
|
|
103
|
+
navigate(-1)
|
|
104
|
+
}
|
|
82
105
|
|
|
83
106
|
const getModelName = (f?: HTMLFormElement) => {
|
|
84
107
|
if (p && p.name) {
|
|
85
|
-
return p.name
|
|
108
|
+
return p.name
|
|
86
109
|
}
|
|
87
|
-
return getModelName2(f,
|
|
88
|
-
}
|
|
110
|
+
return getModelName2(f, "model")
|
|
111
|
+
}
|
|
89
112
|
|
|
90
113
|
const showModel = (model: T) => {
|
|
91
|
-
const n: string = getModelName(refForm.current)
|
|
92
|
-
const objSet: any = {}
|
|
93
|
-
objSet[n] = model
|
|
94
|
-
setState(objSet)
|
|
95
|
-
}
|
|
114
|
+
const n: string = getModelName(refForm.current)
|
|
115
|
+
const objSet: any = {}
|
|
116
|
+
objSet[n] = model
|
|
117
|
+
setState(objSet)
|
|
118
|
+
}
|
|
96
119
|
|
|
97
120
|
const _handleNotFound = (form?: any): void => {
|
|
98
|
-
const msg = message(p1.resource.value,
|
|
121
|
+
const msg = message(p1.resource.value, "error_not_found", "error")
|
|
99
122
|
if (form) {
|
|
100
|
-
setReadOnly(form)
|
|
123
|
+
setReadOnly(form)
|
|
101
124
|
}
|
|
102
|
-
p1.showError(msg.message, msg.title)
|
|
103
|
-
}
|
|
104
|
-
const handleNotFound =
|
|
125
|
+
p1.showError(msg.message, msg.title)
|
|
126
|
+
}
|
|
127
|
+
const handleNotFound = p && p.handleNotFound ? p.handleNotFound : _handleNotFound
|
|
105
128
|
|
|
106
129
|
const _load = (_id: ID, callback?: (m: T, showM: (m2: T) => void) => void) => {
|
|
107
|
-
const id: any = _id
|
|
108
|
-
if (id != null && id !==
|
|
109
|
-
setRunning(true)
|
|
110
|
-
showLoading(p1.loading)
|
|
111
|
-
const fn =
|
|
112
|
-
fn(id)
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
if (callback) {
|
|
117
|
-
callback(obj, showModel);
|
|
130
|
+
const id: any = _id
|
|
131
|
+
if (id != null && id !== "") {
|
|
132
|
+
setRunning(true)
|
|
133
|
+
showLoading(p1.loading)
|
|
134
|
+
const fn = typeof service === "function" ? service : service.load
|
|
135
|
+
fn(id)
|
|
136
|
+
.then((obj) => {
|
|
137
|
+
if (!obj) {
|
|
138
|
+
handleNotFound(refForm.current)
|
|
118
139
|
} else {
|
|
119
|
-
|
|
140
|
+
if (callback) {
|
|
141
|
+
callback(obj, showModel)
|
|
142
|
+
} else {
|
|
143
|
+
showModel(obj)
|
|
144
|
+
}
|
|
120
145
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
146
|
+
setRunning(false)
|
|
147
|
+
hideLoading(p1.loading)
|
|
148
|
+
})
|
|
149
|
+
.catch((err) => {
|
|
150
|
+
const data = err && err.response ? err.response : err
|
|
151
|
+
const r = p1.resource
|
|
152
|
+
const title = r.value("error")
|
|
153
|
+
let msg = r.value("error_internal")
|
|
154
|
+
if (data && data.status === 404) {
|
|
155
|
+
handleNotFound(refForm.current)
|
|
156
|
+
} else {
|
|
157
|
+
if (data && data.status) {
|
|
158
|
+
msg = messageByHttpStatus(data.status, r.value)
|
|
159
|
+
}
|
|
160
|
+
setReadOnly(refForm.current)
|
|
161
|
+
p1.showError(msg, title)
|
|
134
162
|
}
|
|
135
|
-
|
|
136
|
-
p1.
|
|
137
|
-
}
|
|
138
|
-
setRunning(false);
|
|
139
|
-
hideLoading(p1.loading);
|
|
140
|
-
});
|
|
163
|
+
setRunning(false)
|
|
164
|
+
hideLoading(p1.loading)
|
|
165
|
+
})
|
|
141
166
|
}
|
|
142
|
-
}
|
|
143
|
-
const load =
|
|
167
|
+
}
|
|
168
|
+
const load = p && p.load ? p.load : _load
|
|
144
169
|
|
|
145
170
|
return {
|
|
146
171
|
state,
|
|
@@ -153,6 +178,6 @@ export const useCoreView = <T, ID, S>(
|
|
|
153
178
|
getModelName,
|
|
154
179
|
handleNotFound,
|
|
155
180
|
load,
|
|
156
|
-
back
|
|
157
|
-
}
|
|
158
|
-
}
|
|
181
|
+
back,
|
|
182
|
+
}
|
|
183
|
+
}
|
package/src/util.ts
CHANGED
|
@@ -1,120 +1,121 @@
|
|
|
1
|
-
import {Locale} from
|
|
2
|
-
import {enLocale} from
|
|
1
|
+
import { Locale } from "./core"
|
|
2
|
+
import { enLocale } from "./state"
|
|
3
3
|
|
|
4
|
-
const r1 = / |,|\$|€|£|¥|'|٬|،| /g
|
|
5
|
-
const r2 = / |\.|\$|€|£|¥|'|٬|،| /g
|
|
4
|
+
const r1 = / |,|\$|€|£|¥|'|٬|،| /g
|
|
5
|
+
const r2 = / |\.|\$|€|£|¥|'|٬|،| /g
|
|
6
6
|
|
|
7
|
-
export function valueOf(ctrl: HTMLInputElement, locale?: Locale, eventType?: string): { mustChange: any
|
|
8
|
-
if (ctrl.type ===
|
|
9
|
-
const ctrlOnValue = ctrl.getAttribute(
|
|
10
|
-
const ctrlOffValue = ctrl.getAttribute(
|
|
7
|
+
export function valueOf(ctrl: HTMLInputElement, locale?: Locale, eventType?: string): { mustChange: any; value?: any } {
|
|
8
|
+
if (ctrl.type === "checkbox") {
|
|
9
|
+
const ctrlOnValue = ctrl.getAttribute("data-on-value")
|
|
10
|
+
const ctrlOffValue = ctrl.getAttribute("data-off-value")
|
|
11
11
|
if (ctrlOnValue && ctrlOffValue) {
|
|
12
|
-
const onValue = ctrlOnValue ? ctrlOnValue : true
|
|
13
|
-
const offValue = ctrlOffValue ? ctrlOffValue : false
|
|
14
|
-
return ctrl.checked === true ? { mustChange: true, value: onValue } : { mustChange: true, value: offValue }
|
|
12
|
+
const onValue = ctrlOnValue ? ctrlOnValue : true
|
|
13
|
+
const offValue = ctrlOffValue ? ctrlOffValue : false
|
|
14
|
+
return ctrl.checked === true ? { mustChange: true, value: onValue } : { mustChange: true, value: offValue }
|
|
15
15
|
} else {
|
|
16
|
-
return ctrl.checked === true ? { mustChange: true, value: true } : { mustChange: true, value: false }
|
|
16
|
+
return ctrl.checked === true ? { mustChange: true, value: true } : { mustChange: true, value: false }
|
|
17
17
|
}
|
|
18
18
|
} else {
|
|
19
|
-
const datatype = ctrl.getAttribute(
|
|
20
|
-
if (datatype ===
|
|
21
|
-
let v
|
|
22
|
-
if (locale && locale.decimalSeparator !==
|
|
23
|
-
v = ctrl.value.replace(r2,
|
|
19
|
+
const datatype = ctrl.getAttribute("data-type")
|
|
20
|
+
if (datatype === "number" || datatype === "int") {
|
|
21
|
+
let v
|
|
22
|
+
if (locale && locale.decimalSeparator !== ".") {
|
|
23
|
+
v = ctrl.value.replace(r2, "")
|
|
24
24
|
if (v.indexOf(locale.decimalSeparator) >= 0) {
|
|
25
|
-
v = v.replace(locale.decimalSeparator,
|
|
25
|
+
v = v.replace(locale.decimalSeparator, ".")
|
|
26
26
|
}
|
|
27
27
|
} else {
|
|
28
|
-
v = ctrl.value.replace(r1,
|
|
28
|
+
v = ctrl.value.replace(r1, "")
|
|
29
29
|
}
|
|
30
|
-
return isNaN(v as any) ? { mustChange: false } : { mustChange: true, value: parseFloat(v) }
|
|
31
|
-
} else if (datatype ===
|
|
32
|
-
const ml = ctrl.getAttribute(
|
|
33
|
-
const nm =
|
|
34
|
-
const res: any = getStringCurrency(ctrl.value, datatype, locale, nm, eventType ===
|
|
35
|
-
return res
|
|
30
|
+
return isNaN(v as any) ? { mustChange: false } : { mustChange: true, value: parseFloat(v) }
|
|
31
|
+
} else if (datatype === "currency" || datatype === "string-currency") {
|
|
32
|
+
const ml = ctrl.getAttribute("maxlength")
|
|
33
|
+
const nm = ml != null && !isNaN(ml as any) ? parseInt(ml, 10) : undefined
|
|
34
|
+
const res: any = getStringCurrency(ctrl.value, datatype, locale, nm, eventType === "blur")
|
|
35
|
+
return res
|
|
36
36
|
} else {
|
|
37
|
-
return { mustChange: true, value: ctrl.value }
|
|
37
|
+
return { mustChange: true, value: ctrl.value }
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
function getStringCurrency(value: string, datatype: string, locale?: Locale, maxLength?: number, isOnBlur?: boolean): { mustChange: any
|
|
43
|
-
if (locale && locale.decimalSeparator !==
|
|
44
|
-
value = value.replace(r2,
|
|
42
|
+
function getStringCurrency(value: string, datatype: string, locale?: Locale, maxLength?: number, isOnBlur?: boolean): { mustChange: any; value?: string } {
|
|
43
|
+
if (locale && locale.decimalSeparator !== ".") {
|
|
44
|
+
value = value.replace(r2, "")
|
|
45
45
|
if (value.indexOf(locale.decimalSeparator) >= 0) {
|
|
46
|
-
value = value.replace(locale.decimalSeparator,
|
|
46
|
+
value = value.replace(locale.decimalSeparator, ".")
|
|
47
47
|
}
|
|
48
48
|
} else {
|
|
49
|
-
value = value.replace(r1,
|
|
49
|
+
value = value.replace(r1, "")
|
|
50
50
|
}
|
|
51
|
-
if (value ===
|
|
52
|
-
return { mustChange: true, value:
|
|
51
|
+
if (value === "") {
|
|
52
|
+
return { mustChange: true, value: "" }
|
|
53
53
|
}
|
|
54
|
-
value = extractNumber(value,
|
|
54
|
+
value = extractNumber(value, ".") // value = ReactUtil.extractNumber(value, locale.decimalSeparator);
|
|
55
55
|
if (value.length === 0) {
|
|
56
|
-
return { mustChange: false }
|
|
57
|
-
} else if (value.length > 0 && value.charAt(0) ===
|
|
58
|
-
return { mustChange: true, value: value.substring(1) }
|
|
56
|
+
return { mustChange: false }
|
|
57
|
+
} else if (value.length > 0 && value.charAt(0) === "0") {
|
|
58
|
+
return { mustChange: true, value: value.substring(1) }
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
if (!locale) {
|
|
62
|
-
locale = enLocale
|
|
62
|
+
locale = enLocale
|
|
63
63
|
}
|
|
64
|
-
const decimalDigits = locale ? locale.decimalDigits : 2
|
|
65
|
-
const groupDigits = 3
|
|
66
|
-
const decimalSeparator = locale.decimalSeparator
|
|
67
|
-
const groupSeparator = locale.groupSeparator
|
|
64
|
+
const decimalDigits = locale ? locale.decimalDigits : 2
|
|
65
|
+
const groupDigits = 3 // TODO in database locale don't have data
|
|
66
|
+
const decimalSeparator = locale.decimalSeparator // '.'
|
|
67
|
+
const groupSeparator = locale.groupSeparator // ','
|
|
68
68
|
|
|
69
69
|
if (isOnBlur) {
|
|
70
|
-
const number = Number(value.replace(/^0+/,
|
|
70
|
+
const number = Number(value.replace(/^0+/, ""))
|
|
71
71
|
if (number === 0) {
|
|
72
|
-
return { mustChange: true, value:
|
|
72
|
+
return { mustChange: true, value: "" }
|
|
73
73
|
} else {
|
|
74
|
-
value = number.toFixed(decimalDigits)
|
|
74
|
+
value = number.toFixed(decimalDigits)
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
const dotPosition = value.indexOf(
|
|
78
|
+
const dotPosition = value.indexOf(".")
|
|
79
79
|
// Format thousands
|
|
80
|
-
let beforeDot = dotPosition >= 0 ? value.substr(0, dotPosition) : value
|
|
81
|
-
if (datatype ===
|
|
82
|
-
beforeDot = beforeDot.replace(new RegExp(
|
|
80
|
+
let beforeDot = dotPosition >= 0 ? value.substr(0, dotPosition) : value
|
|
81
|
+
if (datatype === "string-currency" || isOnBlur) {
|
|
82
|
+
beforeDot = beforeDot.replace(new RegExp("\\B(?=(\\d{" + groupDigits + "})+(?!\\d))", "g"), groupSeparator)
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
// Cut after dot
|
|
86
|
-
let afterDot
|
|
86
|
+
let afterDot
|
|
87
87
|
if (dotPosition > 0) {
|
|
88
|
-
afterDot = value.substr(dotPosition + 1)
|
|
88
|
+
afterDot = value.substr(dotPosition + 1)
|
|
89
89
|
if (afterDot.length > decimalDigits) {
|
|
90
|
-
afterDot = afterDot.substr(0, decimalDigits)
|
|
90
|
+
afterDot = afterDot.substr(0, decimalDigits)
|
|
91
91
|
}
|
|
92
92
|
}
|
|
93
93
|
if (maxLength && beforeDot.length > maxLength - (decimalDigits + 1)) {
|
|
94
|
-
return { mustChange: false }
|
|
94
|
+
return { mustChange: false }
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
-
value = dotPosition >= 0 ? beforeDot + decimalSeparator + afterDot : beforeDot
|
|
98
|
-
return maxLength && value.length > maxLength ? { mustChange: false } : { mustChange: true, value }
|
|
97
|
+
value = dotPosition >= 0 ? beforeDot + decimalSeparator + afterDot : beforeDot
|
|
98
|
+
return maxLength && value.length > maxLength ? { mustChange: false } : { mustChange: true, value }
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
function extractNumber(str: string, decimalSeparator: string): string {
|
|
102
|
-
const arrs: string[] = []
|
|
103
|
-
let d = false
|
|
102
|
+
const arrs: string[] = []
|
|
103
|
+
let d = false
|
|
104
104
|
for (let i = 0; i < str.length; i++) {
|
|
105
|
-
const ch = str.charAt(i)
|
|
106
|
-
if (ch >=
|
|
107
|
-
arrs.push(ch)
|
|
108
|
-
} else if (ch === decimalSeparator) {
|
|
105
|
+
const ch = str.charAt(i) // get char
|
|
106
|
+
if (ch >= "0" && ch <= "9") {
|
|
107
|
+
arrs.push(ch)
|
|
108
|
+
} else if (ch === decimalSeparator) {
|
|
109
|
+
// else if (ch === '.') {
|
|
109
110
|
if (d) {
|
|
110
|
-
return arrs.join(
|
|
111
|
+
return arrs.join("")
|
|
111
112
|
} else {
|
|
112
|
-
d = true
|
|
113
|
-
arrs.push(ch)
|
|
113
|
+
d = true
|
|
114
|
+
arrs.push(ch)
|
|
114
115
|
}
|
|
115
116
|
} else {
|
|
116
|
-
return arrs.join(
|
|
117
|
+
return arrs.join("")
|
|
117
118
|
}
|
|
118
119
|
}
|
|
119
|
-
return arrs.join(
|
|
120
|
+
return arrs.join("")
|
|
120
121
|
}
|