Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Toolkit
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
digital blueprint
Web Component Framework
Toolkit
Commits
aa9525f3
Commit
aa9525f3
authored
5 years ago
by
Reiter, Christoph
Browse files
Options
Downloads
Patches
Plain Diff
Move utils.js to named exports
So that rollup can do tree shaking and report import errors.
parent
2f811873
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
packages/common/test/unit.js
+1
-1
1 addition, 1 deletion
packages/common/test/unit.js
packages/common/utils.js
+112
-114
112 additions, 114 deletions
packages/common/utils.js
with
113 additions
and
115 deletions
packages/common/test/unit.js
+
1
−
1
View file @
aa9525f3
import
utils
from
'
../utils
'
;
import
*
as
utils
from
'
../utils
'
;
describe
(
'
utils
'
,
()
=>
{
it
(
'
base64EncodeUnicode
'
,
()
=>
{
...
...
This diff is collapsed.
Click to expand it.
packages/common/utils.js
+
112
−
114
View file @
aa9525f3
import
env
from
'
./env.js
'
;
export
default
{
/**
* Parses a link header
*
* The node module parse-link-header didn't work, so https://gist.github.com/niallo/3109252 became handy
*
* @param header
*/
parseLinkHeader
:
(
header
)
=>
{
if
(
header
.
length
===
0
)
{
throw
new
Error
(
"
input must not be of zero length
"
);
}
/**
* Parses a link header
*
* The node module parse-link-header didn't work, so https://gist.github.com/niallo/3109252 became handy
*
* @param header
*/
export
const
parseLinkHeader
=
(
header
)
=>
{
if
(
header
.
length
===
0
)
{
throw
new
Error
(
"
input must not be of zero length
"
);
}
// Split parts by comma
const
parts
=
header
.
split
(
'
,
'
);
const
links
=
{};
// Split parts by comma
const
parts
=
header
.
split
(
'
,
'
);
const
links
=
{};
// Parse each part into a named link
for
(
let
i
=
0
;
i
<
parts
.
length
;
i
++
)
{
const
section
=
parts
[
i
].
split
(
'
;
'
);
if
(
section
.
length
!==
2
)
{
throw
new
Error
(
"
section could not be split on ';'
"
);
}
const
url
=
section
[
0
].
replace
(
/<
(
.*
)
>/
,
'
$1
'
).
trim
();
const
name
=
section
[
1
].
replace
(
/rel="
(
.*
)
"/
,
'
$1
'
).
trim
();
links
[
name
]
=
url
;
// Parse each part into a named link
for
(
let
i
=
0
;
i
<
parts
.
length
;
i
++
)
{
const
section
=
parts
[
i
].
split
(
'
;
'
);
if
(
section
.
length
!==
2
)
{
throw
new
Error
(
"
section could not be split on ';'
"
);
}
const
url
=
section
[
0
].
replace
(
/<
(
.*
)
>/
,
'
$1
'
).
trim
();
const
name
=
section
[
1
].
replace
(
/rel="
(
.*
)
"/
,
'
$1
'
).
trim
();
links
[
name
]
=
url
;
}
return
links
;
},
return
links
;
}
/**
* Reads a setting
*
* @param key
* @returns {*}
*/
setting
:
(
key
)
=>
{
return
env
[
key
];
},
/**
* Reads a setting
*
* @param key
* @returns {*}
*/
export
const
setting
=
(
key
)
=>
{
return
env
[
key
];
}
getAPiUrl
:
(
path
=
""
,
withPrefix
=
true
)
=>
{
return
env
.
apiBaseUrl
+
(
withPrefix
?
env
.
apiUrlPrefix
:
""
)
+
path
;
},
export
const
getAPiUrl
=
(
path
=
""
,
withPrefix
=
true
)
=>
{
return
env
.
apiBaseUrl
+
(
withPrefix
?
env
.
apiUrlPrefix
:
""
)
+
path
;
}
/**
* Parses the base url from an url
*
* @param url
* @returns {string}
*/
parseBaseUrl
:
(
url
)
=>
{
const
pathArray
=
url
.
split
(
'
/
'
);
const
protocol
=
pathArray
[
0
];
const
host
=
pathArray
[
2
];
return
protocol
+
'
//
'
+
host
;
},
/**
* Parses the base url from an url
*
* @param url
* @returns {string}
*/
export
const
parseBaseUrl
=
(
url
)
=>
{
const
pathArray
=
url
.
split
(
'
/
'
);
const
protocol
=
pathArray
[
0
];
const
host
=
pathArray
[
2
];
return
protocol
+
'
//
'
+
host
;
}
/**
* Converts a string list to a data array for Select2
*
* @param list
* @returns {Array}
*/
stringListToSelect2DataArray
:
(
list
)
=>
{
let
data
=
[];
list
.
forEach
((
item
)
=>
{
data
.
push
({
id
:
item
,
text
:
item
})});
return
data
;
},
/**
* Converts a string list to a data array for Select2
*
* @param list
* @returns {Array}
*/
export
const
stringListToSelect2DataArray
=
(
list
)
=>
{
let
data
=
[];
list
.
forEach
((
item
)
=>
{
data
.
push
({
id
:
item
,
text
:
item
})});
return
data
;
}
/**
* Does generic Base64 Encoding with support for 16-bit encoded strings
* @see https://www.base64encoder.io/javascript/
*
* @param str
* @returns {string}
*/
base64EncodeUnicode
:
(
str
)
=>
{
// First we escape the string using encodeURIComponent to get the UTF-8 encoding of the characters,
// then we convert the percent encodings into raw bytes, and finally feed it to btoa() function.
const
utf8Bytes
=
encodeURIComponent
(
str
).
replace
(
/%
([
0-9A-F
]{2})
/g
,
function
(
match
,
p1
)
{
return
String
.
fromCharCode
(
'
0x
'
+
p1
);
});
/**
* Does generic Base64 Encoding with support for 16-bit encoded strings
* @see https://www.base64encoder.io/javascript/
*
* @param str
* @returns {string}
*/
export
const
base64EncodeUnicode
=
(
str
)
=>
{
// First we escape the string using encodeURIComponent to get the UTF-8 encoding of the characters,
// then we convert the percent encodings into raw bytes, and finally feed it to btoa() function.
const
utf8Bytes
=
encodeURIComponent
(
str
).
replace
(
/%
([
0-9A-F
]{2})
/g
,
function
(
match
,
p1
)
{
return
String
.
fromCharCode
(
'
0x
'
+
p1
);
});
return
btoa
(
utf8Bytes
);
},
return
btoa
(
utf8Bytes
);
}
/**
* Like customElements.define() but tries to display an error in case the browser doesn't
* support everything we need.
*
* Returns false in case define failed, true otherwise.
*
* @returns {boolean}
*/
defineCustomElement
:
(
name
,
constructor
,
options
)
=>
{
// Checks taken from https://github.com/webcomponents/webcomponentsjs/blob/master/webcomponents-loader.js
if
(
!
(
'
attachShadow
'
in
Element
.
prototype
&&
'
getRootNode
'
in
Element
.
prototype
&&
window
.
customElements
))
{
var
elements
=
document
.
getElementsByTagName
(
name
);
for
(
var
i
=
0
;
i
<
elements
.
length
;
i
++
)
{
elements
[
i
].
innerHTML
=
"
<span style='border: 1px solid red; font-size: 0.8em;
"
+
"
opacity: 0.5; padding: 0.2em;'>☹ Your browser is not supported ☹</span>
"
;
}
return
false
;
}
customElements
.
define
(
name
,
constructor
,
options
);
return
true
;
},
/**
* Creates a random id
*
* taken from: https://stackoverflow.com/a/1349426/1581487
*
* @param length
* @returns {string}
*/
makeId
:
(
length
)
=>
{
var
result
=
''
;
var
characters
=
'
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
'
;
var
charactersLength
=
characters
.
length
;
for
(
var
i
=
0
;
i
<
length
;
i
++
)
{
result
+=
characters
.
charAt
(
Math
.
floor
(
Math
.
random
()
*
charactersLength
));
/**
* Like customElements.define() but tries to display an error in case the browser doesn't
* support everything we need.
*
* Returns false in case define failed, true otherwise.
*
* @returns {boolean}
*/
export
const
defineCustomElement
=
(
name
,
constructor
,
options
)
=>
{
// Checks taken from https://github.com/webcomponents/webcomponentsjs/blob/master/webcomponents-loader.js
if
(
!
(
'
attachShadow
'
in
Element
.
prototype
&&
'
getRootNode
'
in
Element
.
prototype
&&
window
.
customElements
))
{
var
elements
=
document
.
getElementsByTagName
(
name
);
for
(
var
i
=
0
;
i
<
elements
.
length
;
i
++
)
{
elements
[
i
].
innerHTML
=
"
<span style='border: 1px solid red; font-size: 0.8em;
"
+
"
opacity: 0.5; padding: 0.2em;'>☹ Your browser is not supported ☹</span>
"
;
}
return
false
;
}
customElements
.
define
(
name
,
constructor
,
options
);
return
true
;
}
return
result
;
/**
* Creates a random id
*
* taken from: https://stackoverflow.com/a/1349426/1581487
*
* @param length
* @returns {string}
*/
export
const
makeId
=
(
length
)
=>
{
var
result
=
''
;
var
characters
=
'
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
'
;
var
charactersLength
=
characters
.
length
;
for
(
var
i
=
0
;
i
<
length
;
i
++
)
{
result
+=
characters
.
charAt
(
Math
.
floor
(
Math
.
random
()
*
charactersLength
));
}
};
return
result
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment