@operato/utils 0.3.4 → 0.3.13

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.
@@ -0,0 +1,48 @@
1
+ export function generatePasswordPatternRegExp({
2
+ lowerCase = true,
3
+ upperCase = true,
4
+ digit = true,
5
+ specialCharacter = true,
6
+ allowRepeat = false,
7
+ useTightPattern = true,
8
+ useLoosePattern = false,
9
+ tightCharacterLength = 8,
10
+ looseCharacterLength = 15
11
+ }: {
12
+ lowerCase?: boolean
13
+ upperCase?: boolean
14
+ digit?: boolean
15
+ specialCharacter?: boolean
16
+ allowRepeat?: boolean
17
+ useTightPattern?: boolean
18
+ useLoosePattern?: boolean
19
+ tightCharacterLength?: Number
20
+ looseCharacterLength?: Number
21
+ } = {}) {
22
+ var tightChecklist = useTightPattern
23
+ ? [
24
+ lowerCase ? '(?=.*[a-z])' : '', // has at least one lower case character
25
+ upperCase ? '(?=.*[A-Z])' : '', // has at least one upper case character
26
+ digit ? '(?=.*\\d)' : '', // has at least one digit
27
+ specialCharacter ? '(?=.*[!@#$%^&*()])' : '', // has at least one special character
28
+ !allowRepeat ? '(?!.*(.)\\1(?=\\1{1,}))' : '', // has not an repeated character more than twice
29
+ `.{${tightCharacterLength},}` // has a length of 8 and more
30
+ ]
31
+ : []
32
+
33
+ var looseChecklist = useLoosePattern
34
+ ? [
35
+ `.{${looseCharacterLength},}` // has a length of 15 and more
36
+ ]
37
+ : []
38
+
39
+ var checkList = [
40
+ '^', // from start
41
+ ...tightChecklist,
42
+ tightChecklist.length && looseChecklist.length ? '|' : '',
43
+ ...looseChecklist,
44
+ '$' //to the end"
45
+ ]
46
+
47
+ return new RegExp(checkList.join(''))
48
+ }
@@ -0,0 +1,290 @@
1
+ /**
2
+ * Starts monitoring swipes on the given element and
3
+ * emits `swipe` event when a swipe gesture is performed.
4
+ * @param {DOMElement} element Element on which to listen for swipe gestures.
5
+ * @param {Object} options Optional: Options.
6
+ * @return {Object}
7
+ */
8
+ export function SwipeListener(element: HTMLElement, options?: any) {
9
+ if (!element) return
10
+
11
+ // CustomEvent polyfill
12
+ if (typeof window !== 'undefined') {
13
+ ;(function () {
14
+ if (typeof window.CustomEvent === 'function') return false
15
+ function CustomEvent(event: string, params: any) {
16
+ params = params || { bubbles: false, cancelable: false, detail: undefined }
17
+ var evt = document.createEvent('CustomEvent')
18
+ evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail)
19
+ return evt
20
+ }
21
+ CustomEvent.prototype = window.Event.prototype
22
+ ;(window as any).CustomEvent = CustomEvent
23
+ })()
24
+ }
25
+
26
+ let defaultOpts = {
27
+ minHorizontal: 10, // Minimum number of pixels traveled to count as a horizontal swipe.
28
+ minVertical: 10, // Minimum number of pixels traveled to count as a vertical swipe.
29
+ deltaHorizontal: 3, // Delta for horizontal swipe
30
+ deltaVertical: 5, // Delta for vertical swipe
31
+ preventScroll: false, // Prevents scrolling when swiping.
32
+ lockAxis: true, // Select only one axis to be true instead of multiple.
33
+ touch: true, // Listen for touch events
34
+ mouse: true // Listen for mouse events
35
+ }
36
+
37
+ // Set options
38
+ if (!options) {
39
+ options = {}
40
+ }
41
+ options = {
42
+ ...defaultOpts,
43
+ ...options
44
+ }
45
+
46
+ // Store the touches
47
+ let touches: Array<{ x: number; y: number }> = []
48
+
49
+ // Not dragging by default.
50
+ let dragging = false
51
+
52
+ // When mouse-click is started, make dragging true.
53
+ const _mousedown = function (e: MouseEvent) {
54
+ dragging = true
55
+ }
56
+
57
+ // When mouse-click is released, make dragging false and signify end by imitating `touchend`.
58
+ const _mouseup = function (e: MouseEvent) {
59
+ dragging = false
60
+ _touchend(e as any)
61
+ }
62
+
63
+ // When mouse is moved while being clicked, imitate a `touchmove`.
64
+ const _mousemove = function (e: MouseEvent) {
65
+ if (dragging) {
66
+ ;(e as any).changedTouches = [
67
+ {
68
+ clientX: e.clientX,
69
+ clientY: e.clientY
70
+ }
71
+ ]
72
+ _touchmove(e as any)
73
+ }
74
+ }
75
+
76
+ if (options.mouse) {
77
+ element.addEventListener('mousedown', _mousedown)
78
+ element.addEventListener('mouseup', _mouseup)
79
+ element.addEventListener('mousemove', _mousemove)
80
+ }
81
+
82
+ // When the swipe is completed, calculate the direction.
83
+ const _touchend = function (e: TouchEvent) {
84
+ if (!touches.length) return
85
+
86
+ const touch = typeof TouchEvent === 'function' && e instanceof TouchEvent
87
+
88
+ let x = [],
89
+ y = []
90
+
91
+ let directions = {
92
+ top: false,
93
+ right: false,
94
+ bottom: false,
95
+ left: false
96
+ }
97
+
98
+ for (let i = 0; i < touches.length; i++) {
99
+ x.push(touches[i].x)
100
+ y.push(touches[i].y)
101
+ }
102
+
103
+ const xs = x[0],
104
+ xe = x[x.length - 1], // Start and end x-coords
105
+ ys = y[0],
106
+ ye = y[y.length - 1] // Start and end y-coords
107
+
108
+ const eventCoords = {
109
+ x: [xs, xe],
110
+ y: [ys, ye]
111
+ }
112
+
113
+ if (touches.length > 1) {
114
+ const swipeReleaseEventData = {
115
+ detail: {
116
+ touch,
117
+ target: e.target,
118
+ ...eventCoords
119
+ }
120
+ }
121
+
122
+ let swipeReleaseEvent = new CustomEvent('swiperelease', swipeReleaseEventData)
123
+ element.dispatchEvent(swipeReleaseEvent)
124
+ }
125
+
126
+ // Determine left or right
127
+ let diff = x[0] - x[x.length - 1]
128
+ let swipe = 'none'
129
+ if (diff > 0) {
130
+ swipe = 'left'
131
+ } else {
132
+ swipe = 'right'
133
+ }
134
+
135
+ let min = Math.min(...x),
136
+ max = Math.max(...x),
137
+ _diff
138
+
139
+ // If minimum horizontal distance was travelled
140
+ if (Math.abs(diff) >= options.minHorizontal) {
141
+ switch (swipe) {
142
+ case 'left':
143
+ _diff = Math.abs(min - x[x.length - 1])
144
+ if (_diff <= options.deltaHorizontal) {
145
+ directions.left = true
146
+ }
147
+ break
148
+ case 'right':
149
+ _diff = Math.abs(max - x[x.length - 1])
150
+ if (_diff <= options.deltaHorizontal) {
151
+ directions.right = true
152
+ }
153
+ break
154
+ }
155
+ }
156
+
157
+ // Determine top or bottom
158
+ diff = y[0] - y[y.length - 1]
159
+ swipe = 'none'
160
+ if (diff > 0) {
161
+ swipe = 'top'
162
+ } else {
163
+ swipe = 'bottom'
164
+ }
165
+
166
+ min = Math.min(...y)
167
+ max = Math.max(...y)
168
+
169
+ // If minimum vertical distance was travelled
170
+ if (Math.abs(diff) >= options.minVertical) {
171
+ switch (swipe) {
172
+ case 'top':
173
+ _diff = Math.abs(min - y[y.length - 1])
174
+ if (_diff <= options.deltaVertical) {
175
+ directions.top = true
176
+ }
177
+ break
178
+ case 'bottom':
179
+ _diff = Math.abs(max - y[y.length - 1])
180
+ if (_diff <= options.deltaVertical) {
181
+ directions.bottom = true
182
+ }
183
+ break
184
+ }
185
+ }
186
+
187
+ // Clear touches array.
188
+ touches = []
189
+
190
+ // If there is a swipe direction, emit an event.
191
+ if (directions.top || directions.right || directions.bottom || directions.left) {
192
+ /**
193
+ * If lockAxis is true, determine which axis to select.
194
+ * The axis with the most travel is selected.
195
+ * TODO: Factor in for the orientation of the device
196
+ * and use it as a weight to determine the travel along an axis.
197
+ */
198
+ if (options.lockAxis) {
199
+ if ((directions.left || directions.right) && Math.abs(xs - xe) > Math.abs(ys - ye)) {
200
+ directions.top = directions.bottom = false
201
+ } else if ((directions.top || directions.bottom) && Math.abs(xs - xe) < Math.abs(ys - ye)) {
202
+ directions.left = directions.right = false
203
+ }
204
+ }
205
+
206
+ const eventData = {
207
+ detail: {
208
+ directions,
209
+ touch,
210
+ target: e.target,
211
+ ...eventCoords
212
+ }
213
+ }
214
+
215
+ let event = new CustomEvent('swipe', eventData)
216
+ element.dispatchEvent(event)
217
+ } else {
218
+ let cancelEvent = new CustomEvent('swipecancel', {
219
+ detail: {
220
+ touch,
221
+ target: e.target,
222
+ ...eventCoords
223
+ }
224
+ })
225
+ element.dispatchEvent(cancelEvent)
226
+ }
227
+ }
228
+
229
+ // When a swipe is performed, store the coords.
230
+ const _touchmove = function (e: TouchEvent) {
231
+ let touch = e.changedTouches[0]
232
+ touches.push({
233
+ x: touch.clientX,
234
+ y: touch.clientY
235
+ })
236
+
237
+ // Emit a `swiping` event if there are more than one touch-points.
238
+ if (touches.length > 1) {
239
+ const xs = touches[0].x, // Start and end x-coords
240
+ xe = touches[touches.length - 1].x,
241
+ ys = touches[0].y, // Start and end y-coords
242
+ ye = touches[touches.length - 1].y,
243
+ eventData = {
244
+ detail: {
245
+ x: [xs, xe],
246
+ y: [ys, ye],
247
+ touch: typeof TouchEvent === 'function' && e instanceof TouchEvent,
248
+ target: e.target
249
+ }
250
+ }
251
+ let event = new CustomEvent('swiping', eventData)
252
+
253
+ const shouldPrevent =
254
+ options.preventScroll === true || (typeof options.preventScroll === 'function' && options.preventScroll(event))
255
+
256
+ if (shouldPrevent) {
257
+ e.preventDefault()
258
+ }
259
+
260
+ element.dispatchEvent(event)
261
+ }
262
+ }
263
+
264
+ // Test via a getter in the options object to see if the passive property is accessed
265
+ let passiveOptions: any = false
266
+ try {
267
+ const testOptions = Object.defineProperty({}, 'passive', {
268
+ get: function () {
269
+ passiveOptions = { passive: !options.preventScroll }
270
+ }
271
+ })
272
+ window.addEventListener('testPassive', null as any, testOptions)
273
+ window.removeEventListener('testPassive', null as any, testOptions)
274
+ } catch (e) {}
275
+
276
+ if (options.touch) {
277
+ element.addEventListener('touchmove', _touchmove, passiveOptions)
278
+ element.addEventListener('touchend', _touchend)
279
+ }
280
+
281
+ return {
282
+ off: function () {
283
+ element.removeEventListener('touchmove', _touchmove, passiveOptions)
284
+ element.removeEventListener('touchend', _touchend)
285
+ element.removeEventListener('mousedown', _mousedown)
286
+ element.removeEventListener('mouseup', _mouseup)
287
+ element.removeEventListener('mousemove', _mousemove)
288
+ }
289
+ }
290
+ }