FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
processing-website/src/utils/index.js at v4 · processing/processing-website · GitHub
processing
processing-website
Repository navigation
Code
Issues
112
(112)
Pull requests
16
(16)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
processing-website
/
src
/
utils
/
index.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
173 lines (150 loc) · 4.31 KB
Breadcrumbs
processing-website
/
src
/
utils
/
index.js
Copy path
File metadata and controls
173 lines (150 loc) · 4.31 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import
{
titleCase
as
_titleCase
}
from
'title-case'
;
/**
Array for turning a date.getMonth() index into a date string
**/
export
const
months
=
[
'January'
,
'February'
,
'March'
,
'April'
,
'May'
,
'June'
,
'July'
,
'August'
,
'September'
,
'October'
,
'November'
,
'December'
]
;
/**
Turns < > & into their unicode equivalents
**/
export
const
escapeHtml
=
(
str
)
=>
str
.
replace
(
/
&
/
g
,
'&'
)
.
replace
(
/
<
/
g
,
'<'
)
.
replace
(
/
>
/
g
,
'>'
)
;
/**
Turns a slug into a title
Example: this_is_something => This Is Something
**/
export
const
titleCase
=
(
slug
)
=>
_titleCase
(
slug
.
replace
(
/
_
/
g
,
' '
)
)
;
/**
Turns a title into a slug
Example: This Is Something => this-is-something
**/
export
const
slugify
=
(
...
titles
)
=>
titles
.
join
(
'-'
)
.
replace
(
/
(
\s
|
_
)
/
g
,
'-'
)
.
toLowerCase
(
)
;
/**
Used to convert a string to an id for intl
"Data Something" > "DataSomething"
**/
export
const
toIntlId
=
(
str
)
=>
str
.
replace
(
/
_
/
g
,
' '
)
.
replace
(
/
/
g
,
''
)
;
export
const
shuffleArray
=
(
array
)
=>
{
for
(
let
i
=
array
.
length
-
1
;
i
>
0
;
i
--
)
{
const
j
=
Math
.
floor
(
Math
.
random
(
)
*
(
i
+
1
)
)
;
[
array
[
i
]
,
array
[
j
]
]
=
[
array
[
j
]
,
array
[
i
]
]
;
}
return
array
;
}
;
export
const
map
=
(
n
,
start1
,
stop1
,
start2
,
stop2
)
=>
{
const
prog
=
Math
.
min
(
1
,
Math
.
max
(
0
,
(
n
-
start1
)
/
(
stop1
-
start1
)
)
)
;
return
prog
*
(
stop2
-
start2
)
+
start2
;
}
;
export
const
getWin
=
(
)
=>
{
if
(
typeof
window
==
'undefined'
)
return
[
null
,
null
]
;
return
[
window
.
innerWidth
,
window
.
innerHeight
]
;
}
;
const
WIDONT_REGEX
=
/
(
[
^
\s
]
)
\s
+
(
[
^
\s
]
+
)
\s
*
$
/
;
const
DASH_REGEX
=
/
-
/
g
;
const
SPACE
=
'\u00a0'
;
//' ';
const
HYPHEN
=
'\u2011'
;
//'‑'
export
const
widont
=
(
str
)
=>
{
if
(
typeof
str
!==
'string'
)
{
return
str
;
}
return
str
.
replace
(
WIDONT_REGEX
,
(
str
,
lead
,
word
)
=>
{
if
(
word
.
indexOf
(
'-'
)
>=
0
)
{
return
lead
+
' '
+
word
.
replace
(
DASH_REGEX
,
HYPHEN
)
;
}
return
lead
+
SPACE
+
word
;
}
)
;
}
;
export
const
linkParsing
=
(
str
)
=>
{
if
(
typeof
str
!==
'string'
)
{
return
str
;
}
return
str
.
replace
(
/
\[
(
[
^
\]
]
*
)
\]
\(
(
[
^
)
]
*
)
\)
/
g
,
'<a href="$2">$1</a>'
)
;
}
;
/**
Gets sessionStorage if available without failing in the server
**/
export
const
sessionStorage
=
typeof
window
!==
'undefined'
?
window
.
sessionStorage
:
null
;
/**
Gets localStorage if available without failing in the server
**/
export
const
localStorage
=
typeof
window
!==
'undefined'
?
window
.
localStorage
:
null
;
/**
Returns a copy of the object with the keys sorted based on the array provided.
**/
export
const
sortObject
=
(
obj
,
order
)
=>
{
const
keys
=
obj
?
Object
.
keys
(
obj
)
.
sort
(
(
a
,
b
)
=>
{
let
aidx
=
order
.
indexOf
(
a
)
;
let
bidx
=
order
.
indexOf
(
b
)
;
// Ensure keys not in order are sorted last
if
(
aidx
===
-
1
)
aidx
=
9999
;
if
(
bidx
===
-
1
)
bidx
=
9999
;
return
aidx
-
bidx
;
}
)
:
[
]
;
const
copy
=
{
}
;
for
(
let
i
=
0
;
i
<
keys
.
length
;
i
++
)
{
copy
[
keys
[
i
]
]
=
obj
[
keys
[
i
]
]
;
}
return
copy
;
}
;
/**
Returns a copy of the array with the items sorted based on the values in the array provided and the key
**/
export
const
sortArray
=
(
arr
,
order
,
key
)
=>
{
return
arr
.
slice
(
)
.
sort
(
(
a
,
b
)
=>
{
let
aidx
=
order
.
indexOf
(
a
[
key
]
)
;
let
bidx
=
order
.
indexOf
(
b
[
key
]
)
;
// Ensure keys not in order are sorted last
if
(
aidx
===
-
1
)
aidx
=
9999
;
if
(
bidx
===
-
1
)
bidx
=
9999
;
return
aidx
-
bidx
;
}
)
;
}
;
// taken from https://www.npmjs.com/package/truncate
export
const
truncate
=
(
string
,
maxLength
)
=>
{
const
URL_REGEX
=
/
(
(
(
f
t
p
|
h
t
t
p
s
?
)
:
\/
\/
)
[
-
\w
@
:
%
_
+
.
~
#
?
,
&
/
/
=
]
+
)
|
(
(
m
a
i
l
t
o
:
)
?
[
_
.
\w
-
]
{
1
,
300
}
@
(
.
{
1
,
300
}
\.
)
[
a
-
z
A
-
Z
]
{
2
,
3
}
)
/
g
;
let
content
=
''
,
// truncated text storage
matches
=
true
,
remainingLength
=
maxLength
,
result
,
index
;
if
(
!
string
||
string
.
length
===
0
)
{
return
''
;
}
matches
=
true
;
while
(
matches
)
{
URL_REGEX
.
lastIndex
=
content
.
length
;
matches
=
URL_REGEX
.
exec
(
string
)
;
if
(
!
matches
||
matches
.
index
-
content
.
length
>=
remainingLength
)
{
content
+=
string
.
substring
(
content
.
length
,
maxLength
)
;
break
;
}
result
=
matches
[
0
]
;
index
=
matches
.
index
;
content
+=
string
.
substring
(
content
.
length
,
index
+
result
.
length
)
;
remainingLength
-=
index
+
result
.
length
;
if
(
remainingLength
<=
0
)
{
break
;
}
}
return
string
.
length
===
content
.
length
?
content
:
content
+
'…'
;
}
;
Back
|
FazBrowse Home
|
New Git URL