1
- /**
2
- * Strip object from its original. (useful for object copy)
3
- *
4
- * @param object
5
- * @return {{} }
6
- */
7
- strip: function ( object ) {
8
- if ( ! object ) return { } ;
9
- return JSON . parse ( JSON . stringify ( object ) ) ;
10
- } ,
11
-
12
- /**
13
- *
14
- * @param array - target array
15
- * @param key - key name of object
16
- * @param value - matched value of given key
17
- * @returns {* }
18
- */
19
- findObjectByKey : function ( array , key , value ) {
20
- for ( var i = 0 ; i < array . length ; i ++ ) {
21
- if ( array [ i ] [ key ] === value ) {
22
- return array [ i ] ;
23
- }
24
- }
25
- return null ;
26
- } ,
27
-
28
- /**
29
- *
30
- * @param array - target array
31
- * @param key - key name of object
32
- * @param value - matched value of given key
33
- * @returns {* }
34
- */
35
- deleteObjectByKey : function ( array , key , value ) {
36
- if ( array . length === 0 ) return ;
37
- for ( var i = 0 ; i < array . length ; i ++ ) {
38
- if ( array [ i ] [ key ] === value ) {
39
- array . splice ( i , 1 ) ;
40
- }
41
- }
42
- return null ;
43
- } ,
44
-
45
- /**
46
- *
47
- * @param url to check
48
- * @returns {boolean }
49
- */
50
- isValidUrl : function ( url ) {
51
- var r = new RegExp ( / ^ ( h t t p : \/ \/ w w w \. | h t t p s : \/ \/ w w w \. | h t t p : \/ \/ | h t t p s : \/ \/ ) ? [ a - z 0 - 9 ] + ( [ \- \. ] { 1 } [ a - z 0 - 9 ] + ) * \. [ a - z ] { 2 , 5 } ( : [ 0 - 9 ] { 1 , 5 } ) ? ( \/ .* ) ? $ / ) ;
52
- return r . test ( url ) ;
53
- } ,
54
-
55
- /**
56
- * Truncate string to set number of characters.
57
- *
58
- * @param text
59
- * @param n
60
- * @return {* }
61
- */
62
- trunc : function ( text , n ) {
63
- if ( ! text ) return '' ;
64
-
65
- return text . length > n ? text . substr ( 0 , n - 3 ) + '...' : text ;
66
- } ,
67
-
68
- /**
69
- * Add comma after every three decimals in a number.
70
- *
71
- * @param value
72
- * @return {* }
73
- */
74
- commaSeparateNumber : function ( value ) {
75
- while ( / ( \d + ) ( \d { 3 } ) / . test ( value . toString ( ) ) ) {
76
- value = value . toString ( ) . replace ( / ( \d + ) ( \d { 3 } ) / , '$1' + ',' + '$2' ) ;
77
- }
78
- return value ;
79
- } ,
80
-
81
- /**
82
- * Check if object is empty.
83
- *
84
- * @param obj
85
- * @return {boolean }
86
- */
87
- isObjectEmpty ( obj ) {
88
- for ( var prop in obj ) {
89
- if ( obj . hasOwnProperty ( prop ) ) {
90
- return false ;
91
- }
92
- }
93
- return true ;
94
- } ,
95
-
96
- /**
97
- * Round value to given amount of decimals.
98
- *
99
- * @param value
100
- * @param decimals
101
- * @return {number }
102
- */
103
- round : function ( value , decimals ) {
104
- let multiplier = 1 ;
105
- decimals = decimals || 8 ;
106
-
107
- multiplier = 1 ;
108
-
109
- while ( ( multiplier + '' ) . length <= decimals ) {
110
- multiplier = multiplier * 10 ;
111
- }
112
-
113
- return Math . round ( parseFloat ( value ) * multiplier ) / multiplier ;
114
- } ,
115
-
116
- /**
117
- * Copy element value to clipboard.
118
- *
119
- * @param id
120
- * @return {boolean }
121
- */
122
- copyToClipboard : function ( text ) {
123
- if ( window . clipboardData && window . clipboardData . setData ) {
124
- // IE specific code path to prevent textarea being shown while dialog is visible.
125
- return window . clipboardData . setData ( 'Text' , text ) ;
126
- } else if ( document . queryCommandSupported && document . queryCommandSupported ( 'copy' ) ) {
127
- var textarea = document . createElement ( 'textarea' ) ;
128
- textarea . textContent = text ;
129
- textarea . style . position = 'fixed' ; // Prevent scrolling to bottom of page in MS Edge.
130
- document . body . appendChild ( textarea ) ;
131
- textarea . select ( ) ;
132
- try {
133
- return document . execCommand ( 'copy' ) ; // Security exception may be thrown by some browsers.
134
- } catch ( err ) {
135
- this . sayHello ( 'Copy to clipboard failed.' , err ) ; // TOAST
136
- return false ;
137
- } finally {
138
- document . body . removeChild ( textarea ) ;
139
- }
140
- }
1
+ /**
2
+ * Strip object from its original. (useful for object copy)
3
+ *
4
+ * @param object
5
+ * @return {{} }
6
+ */
7
+ export const strip = ( object ) => {
8
+ if ( ! object ) return { } ;
9
+ return JSON . parse ( JSON . stringify ( object ) ) ;
10
+ } ;
11
+
12
+ /**
13
+ *
14
+ * @param array - target array
15
+ * @param key - key name of object
16
+ * @param value - matched value of given key
17
+ * @returns {* }
18
+ */
19
+ export const findObjectByKey = ( array , key , value ) => {
20
+ for ( var i = 0 ; i < array . length ; i ++ ) {
21
+ if ( array [ i ] [ key ] === value ) {
22
+ return array [ i ] ;
23
+ }
24
+ }
25
+ return null ;
26
+ } ;
27
+
28
+ /**
29
+ *
30
+ * @param array - target array
31
+ * @param key - key name of object
32
+ * @param value - matched value of given key
33
+ * @returns {* }
34
+ */
35
+ export const deleteObjectByKey = ( array , key , value ) => {
36
+ if ( array . length === 0 ) return ;
37
+ for ( var i = 0 ; i < array . length ; i ++ ) {
38
+ if ( array [ i ] [ key ] === value ) {
39
+ array . splice ( i , 1 ) ;
40
+ }
41
+ }
42
+ return null ;
43
+ } ;
44
+
45
+ /**
46
+ *
47
+ * @param url to check
48
+ * @returns {boolean }
49
+ */
50
+ export const isValidUrl = ( url ) => {
51
+ var r = new RegExp (
52
+ / ^ ( h t t p : \/ \/ w w w \. | h t t p s : \/ \/ w w w \. | h t t p : \/ \/ | h t t p s : \/ \/ ) ? [ a - z 0 - 9 ] + ( [ \- \. ] { 1 } [ a - z 0 - 9 ] + ) * \. [ a - z ] { 2 , 5 } ( : [ 0 - 9 ] { 1 , 5 } ) ? ( \/ .* ) ? $ /
53
+ ) ;
54
+ return r . test ( url ) ;
55
+ } ;
56
+
57
+ /**
58
+ * Truncate string to set number of characters.
59
+ *
60
+ * @param text
61
+ * @param n
62
+ * @return {* }
63
+ */
64
+ export const trunc = ( text , n ) => {
65
+ if ( ! text ) return '' ;
66
+ return text . length > n ? text . substr ( 0 , n - 3 ) + '...' : text ;
67
+ } ;
68
+
69
+ /**
70
+ * Add comma after every three decimals in a number.
71
+ *
72
+ * @param value
73
+ * @return {* }
74
+ */
75
+ export const commaSeparateNumber = ( value ) => {
76
+ while ( / ( \d + ) ( \d { 3 } ) / . test ( value . toString ( ) ) ) {
77
+ value = value . toString ( ) . replace ( / ( \d + ) ( \d { 3 } ) / , '$1' + ',' +
78
+ '$2' ) ;
79
+ }
80
+ return value ;
81
+ } ;
82
+
83
+ /**
84
+ * Check if object is empty.
85
+ *
86
+ * @param obj
87
+ * @return {boolean }
88
+ */
89
+ export const isObjectEmpty = ( obj ) => {
90
+ for ( var prop in obj ) {
91
+ if ( obj . hasOwnProperty ( prop ) ) {
92
+ return false ;
141
93
}
94
+ }
95
+ return true ;
96
+ } ;
97
+
98
+ /**
99
+ * Round value to given amount of decimals.
100
+ *
101
+ * @param value
102
+ * @param decimals
103
+ * @return {number }
104
+ */
105
+ export const round = ( value , decimals ) => {
106
+ let multiplier = 1 ;
107
+ decimals = decimals || 8 ;
108
+
109
+ multiplier = 1 ;
110
+
111
+ while ( ( multiplier + '' ) . length <= decimals ) {
112
+ multiplier = multiplier * 10 ;
113
+ }
114
+
115
+ return Math . round ( parseFloat ( value ) * multiplier ) /
116
+ multiplier ;
117
+ } ;
118
+
119
+ /**
120
+ * Copy element value to clipboard.
121
+ *
122
+ * @param id
123
+ * @return {boolean }
124
+ */
125
+ export const copyToClipboard = function ( text ) => {
126
+ if ( window . clipboardData && window . clipboardData . setData ) {
127
+ // IE specific code path to prevent textarea being shown while dialog is visible.
128
+ return window . clipboardData . setData ( 'Text' , text ) ;
129
+ } else if ( document . queryCommandSupported && document . queryCommandSupported (
130
+ 'copy' ) ) {
131
+ var textarea = document . createElement ( 'textarea' ) ;
132
+ textarea . textContent = text ;
133
+ textarea . style . position = 'fixed' ; // Prevent scrolling to bottom of page in MS Edge.
134
+ document . body . appendChild ( textarea ) ;
135
+ textarea . select ( ) ;
136
+ try {
137
+ return document . execCommand ( 'copy' ) ; // Security exception may be thrown by some browsers.
138
+ } catch ( err ) {
139
+ this . sayHello ( 'Copy to clipboard failed.' , err ) ; // TOAST
140
+ return false ;
141
+ } finally {
142
+ document . body . removeChild ( textarea ) ;
143
+ }
144
+ }
145
+ } ;
146
+ /**
147
+ * Production friendly console log.
148
+ * @param content
149
+ */
150
+ export const log = ( content ) => {
151
+ if ( process . env !== 'production' ) {
152
+ console . log ( content ) ;
153
+ }
154
+ } ;
155
+
156
+ /**
157
+ * Sum all items in arrray.
158
+ * @param arr
159
+ */
160
+ export const sumArray = ( arr ) => {
161
+ let sum = null ;
162
+ for ( let i of arr ) {
163
+ sum = sum + i ;
164
+ }
165
+ return sum ;
166
+ } ;
167
+
168
+
169
+ /**
170
+ * Native funciton to lodash's _.uniq
171
+ * IE11+
172
+ * @param arr
173
+ */
174
+ export const uniq = ( arr ) => {
175
+ return [ ...Set ( arr ) ] ;
176
+ }
177
+
178
+
179
+ /**
180
+ * Native funciton to lodash's _.fromPairs
181
+ * @param arr
182
+ */
183
+ export const fromPairs = ( arr ) => arr . reduce ( ( acc , val ) => ( acc [ val [ 0 ] ] = val [ 1 ] ,
184
+ acc ) , { } )
0 commit comments