@operato/utils 1.0.0-alpha.9 → 1.0.0-beta.10

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/format.ts ADDED
@@ -0,0 +1,108 @@
1
+ /*
2
+ * Source Code from https://github.com/Mottie/javascript-number-formatter
3
+ * 소스코드 출처 : https://github.com/Mottie/javascript-number-formatter
4
+ */
5
+
6
+ export function format(mask: any, value: any): string {
7
+ if (!mask || isNaN(+value)) {
8
+ return value // return as it is.
9
+ }
10
+
11
+ var isNegative,
12
+ result,
13
+ decimal,
14
+ group,
15
+ posLeadZero,
16
+ posTrailZero,
17
+ posSeparator,
18
+ part,
19
+ szSep,
20
+ integer,
21
+ // find prefix/suffix
22
+ len = mask.length,
23
+ start = mask.search(/[0-9\-\+#]/),
24
+ prefix = start > 0 ? mask.substring(0, start) : '',
25
+ // reverse string: not an ideal method if there are surrogate pairs
26
+ str = mask.split('').reverse().join(''),
27
+ end = str.search(/[0-9\-\+#]/),
28
+ offset = len - end,
29
+ substr = mask.substring(offset, offset + 1),
30
+ indx = offset + (substr === '.' || substr === ',' ? 1 : 0),
31
+ suffix = end > 0 ? mask.substring(indx, len) : ''
32
+
33
+ // mask with prefix & suffix removed
34
+ mask = mask.substring(start, indx)
35
+
36
+ // convert any string to number according to formation sign.
37
+ value = mask.charAt(0) === '-' ? -value : +value
38
+ isNegative = value < 0 ? (value = -value) : 0 // process only abs(), and turn on flag.
39
+
40
+ // search for separator for grp & decimal, anything not digit, not +/- sign, not #.
41
+ result = mask.match(/[^\d\-\+#]/g)
42
+ decimal = '.' // ( result && result[ result.length - 1 ] ) || '.'; // ','는 소수점이 되지 않게 함
43
+ group = (result && result[1] && result[0]) || ',' // treat the left most symbol as group separator
44
+
45
+ // split the decimal for the format string if any.
46
+ mask = mask.split(decimal)
47
+ // Fix the decimal first, toFixed will auto fill trailing zero.
48
+ value = value.toFixed(mask[1] && mask[1].length)
49
+ value = +value + '' // convert number to string to trim off *all* trailing decimal zero(es)
50
+
51
+ // fill back any trailing zero according to format
52
+ posTrailZero = mask[1] && mask[1].lastIndexOf('0') // look for last zero in format
53
+ part = value.split('.')
54
+ // integer will get !part[1]
55
+ if (!part[1] || (part[1] && part[1].length <= posTrailZero)) {
56
+ value = (+value).toFixed(posTrailZero + 1)
57
+ }
58
+ szSep = mask[0].split(group) // look for separator
59
+ mask[0] = szSep.join('') // join back without separator for counting the pos of any leading 0.
60
+
61
+ posLeadZero = mask[0] && mask[0].indexOf('0')
62
+ if (posLeadZero > -1) {
63
+ while (part[0].length < mask[0].length - posLeadZero) {
64
+ part[0] = '0' + part[0]
65
+ }
66
+ } else if (+part[0] === 0) {
67
+ part[0] = ''
68
+ }
69
+
70
+ value = value.split('.')
71
+ value[0] = part[0]
72
+
73
+ // process the first group separator from decimal (.) only, the rest ignore.
74
+ // get the length of the last slice of split result.
75
+ posSeparator = szSep[1] && szSep[szSep.length - 1].length
76
+ if (posSeparator) {
77
+ integer = value[0]
78
+ str = ''
79
+ offset = integer.length % posSeparator
80
+ len = integer.length
81
+ for (indx = 0; indx < len; indx++) {
82
+ str += integer.charAt(indx) // ie6 only support charAt for sz.
83
+ // -posSeparator so that won't trail separator on full length
84
+ /*jshint -W018 */
85
+ if (!((indx - offset + 1) % posSeparator) && indx < len - posSeparator) {
86
+ str += group
87
+ }
88
+ }
89
+ value[0] = str
90
+ }
91
+ value[1] = mask[1] && value[1] ? decimal + value[1] : ''
92
+
93
+ // remove negative sign if result is zero
94
+ result = value.join('')
95
+ if (result === '0' || result === '') {
96
+ // remove negative sign if result is zero
97
+ isNegative = false
98
+ }
99
+
100
+ // 앞에 +가 붙는다면 양수일 경우에도 +를 표기해줌
101
+ var fixedPlusSign
102
+
103
+ if (mask[0].substring(0, 1) == '+') fixedPlusSign = isNegative ? '-' : '+'
104
+ else fixedPlusSign = isNegative ? '-' : ''
105
+
106
+ // put back any negation, combine integer and fraction, and add back prefix & suffix
107
+ return prefix + (fixedPlusSign + result) + suffix
108
+ }
package/src/index.ts CHANGED
@@ -6,4 +6,8 @@ export * from './swipe-listener.js'
6
6
  export * from './fullscreen.js'
7
7
  export * from './parse-jwt.js'
8
8
  export * from './password-pattern.js'
9
- export * from './closest-element'
9
+ export * from './closest-element.js'
10
+ export * from './detect-overflow.js'
11
+ export * from './timecapsule/index.js'
12
+ export * from './clipboard.js'
13
+ export * from './format.js'
package/src/logger.ts ADDED
@@ -0,0 +1,21 @@
1
+ /*
2
+ * Copyright © HatioLab Inc. All rights reserved.
3
+ */
4
+
5
+ const ERROR = '[ERROR]'
6
+ const WARN = '[WARN]'
7
+ const DEBUG = '[DEBUG]'
8
+
9
+ export var error = (...args: any[]) => {
10
+ var trace = [] as string[]
11
+ args.forEach(arg => arg && arg.stack && trace.push(arg.stack))
12
+ console.error(ERROR, ...args, trace.join(' '))
13
+ }
14
+
15
+ export var warn = (...args: any[]) => {
16
+ var trace = [] as string[]
17
+ args.forEach(arg => arg && arg.stack && trace.push(arg.stack))
18
+ console.warn(WARN, ...args, trace.join(' '))
19
+ }
20
+
21
+ export var debug = (...args: any[]) => console.log(DEBUG, ...args)
@@ -0,0 +1,2 @@
1
+ export * from './timecapsule.js'
2
+ export * from './snapshot-taker.js'
@@ -0,0 +1,68 @@
1
+ /*
2
+ * Copyright © HatioLab Inc. All rights reserved.
3
+ */
4
+
5
+ import { TimeCapsule } from './timecapsule'
6
+ import debounce from 'lodash-es/debounce'
7
+
8
+ var debouncer = debounce(
9
+ taker => {
10
+ if (!taker.brake) {
11
+ taker.take()
12
+ }
13
+ },
14
+ 1000,
15
+ { leading: true, trailing: false }
16
+ )
17
+
18
+ function take_snapshot(taker: SnapshotTaker) {
19
+ if (taker.brake || !taker.dirty) return
20
+ debouncer(taker)
21
+ }
22
+
23
+ export type StateHolder = {
24
+ state: any
25
+ }
26
+
27
+ export class SnapshotTaker {
28
+ private _brake: boolean = false
29
+
30
+ dirty: boolean = false
31
+
32
+ state_holder?: StateHolder
33
+ timecapsule?: TimeCapsule
34
+
35
+ constructor(state_holder: any, timecapsule: TimeCapsule) {
36
+ this.state_holder = state_holder
37
+ this.timecapsule = timecapsule
38
+ this.timecapsule.snapshot_taker = this
39
+ }
40
+
41
+ dispose() {
42
+ delete this.state_holder
43
+ delete this.timecapsule
44
+ }
45
+
46
+ touch() {
47
+ this.dirty = true
48
+ take_snapshot(this)
49
+ }
50
+
51
+ /* 모든 조건에 관계없이 현재 상태를 snapshot으로 취한다. */
52
+ take(force: boolean = false) {
53
+ if (this.dirty || force) {
54
+ this.timecapsule?.snapshot(this.state_holder?.state)
55
+ this.dirty = false
56
+ }
57
+ }
58
+
59
+ get brake() {
60
+ return this._brake
61
+ }
62
+
63
+ /* 마우스를 드래깅하는 동안, 보통 brake 를 ON 시킨다. */
64
+ set brake(brake) {
65
+ this._brake = !!brake
66
+ take_snapshot(this)
67
+ }
68
+ }
@@ -0,0 +1,93 @@
1
+ /*
2
+ * Copyright © HatioLab Inc. All rights reserved.
3
+ */
4
+
5
+ import { debug, error, warn } from '../logger'
6
+
7
+ import { SnapshotTaker } from './snapshot-taker'
8
+
9
+ export class TimeCapsule {
10
+ private q: Array<any> = []
11
+ private maxsize = 10
12
+ private pos = -1
13
+
14
+ private _snapshot_taker: SnapshotTaker | null = null
15
+
16
+ constructor(maxsize: number, start_state?: any) {
17
+ maxsize = Number(maxsize)
18
+
19
+ if (!maxsize || maxsize <= 0) {
20
+ error('TimeCapsule maxsize should be greater than 0.', maxsize)
21
+ } else {
22
+ this.maxsize = maxsize
23
+ }
24
+
25
+ this.reset()
26
+ if (start_state) {
27
+ this.snapshot(start_state)
28
+ }
29
+ }
30
+
31
+ dispose() {
32
+ this.reset()
33
+ }
34
+
35
+ snapshot(state: any) {
36
+ this.q.splice(this.pos + 1, this.q.length - (this.pos + 1), state)
37
+
38
+ if (this.q.length > this.maxsize) {
39
+ this.q.splice(0, this.q.length - this.maxsize)
40
+ }
41
+
42
+ this.pos = this.q.length - 1
43
+ }
44
+
45
+ forward() {
46
+ if (this.snapshot_taker) this.snapshot_taker.take()
47
+
48
+ if (this.forwardable) {
49
+ return this.q[++this.pos]
50
+ }
51
+ warn('Not forwardable.')
52
+ }
53
+
54
+ backward() {
55
+ if (this.snapshot_taker) this.snapshot_taker.take()
56
+
57
+ if (this.backwardable) {
58
+ return this.q[--this.pos]
59
+ }
60
+ warn('Not backwardable.')
61
+ }
62
+
63
+ get current() {
64
+ if (this.pos !== -1) return this.q[this.pos]
65
+
66
+ warn('Non state has been recorded.')
67
+ }
68
+
69
+ get length() {
70
+ return this.q.length
71
+ }
72
+
73
+ get forwardable() {
74
+ return this.pos < this.q.length - 1
75
+ }
76
+
77
+ get backwardable() {
78
+ return this.pos > 0
79
+ }
80
+
81
+ get snapshot_taker(): SnapshotTaker | null {
82
+ return this._snapshot_taker
83
+ }
84
+
85
+ set snapshot_taker(snapshot_taker: SnapshotTaker | null) {
86
+ this._snapshot_taker = snapshot_taker
87
+ }
88
+
89
+ reset() {
90
+ this.q = []
91
+ this.pos = -1
92
+ }
93
+ }