aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/utils/strings.js
diff options
context:
space:
mode:
authorSam Potts <sam@potts.es>2019-01-29 21:34:40 +1100
committerSam Potts <sam@potts.es>2019-01-29 21:34:40 +1100
commitb1da599b5d5891dc1dca44bd6aa9d8d03872fdcb (patch)
treec799fb2b444482f6d99dcdf3f16a957b290888c0 /src/js/utils/strings.js
parentafc969bac322f9b17dc0554a65fa848eb998c8e6 (diff)
parentb798368ba68853558819d79a995aa0deec27f95e (diff)
downloadplyr-b1da599b5d5891dc1dca44bd6aa9d8d03872fdcb.tar.lz
plyr-b1da599b5d5891dc1dca44bd6aa9d8d03872fdcb.tar.xz
plyr-b1da599b5d5891dc1dca44bd6aa9d8d03872fdcb.zip
Merge branch 'develop' into beta
Diffstat (limited to 'src/js/utils/strings.js')
-rw-r--r--src/js/utils/strings.js85
1 files changed, 85 insertions, 0 deletions
diff --git a/src/js/utils/strings.js b/src/js/utils/strings.js
new file mode 100644
index 00000000..6b9a65a2
--- /dev/null
+++ b/src/js/utils/strings.js
@@ -0,0 +1,85 @@
+// ==========================================================================
+// String utils
+// ==========================================================================
+
+import is from './is';
+
+// Generate a random ID
+export function generateId(prefix) {
+ return `${prefix}-${Math.floor(Math.random() * 10000)}`;
+}
+
+// Format string
+export function format(input, ...args) {
+ if (is.empty(input)) {
+ return input;
+ }
+
+ return input.toString().replace(/{(\d+)}/g, (match, i) => args[i].toString());
+}
+
+// Get percentage
+export function getPercentage(current, max) {
+ if (current === 0 || max === 0 || Number.isNaN(current) || Number.isNaN(max)) {
+ return 0;
+ }
+
+ return ((current / max) * 100).toFixed(2);
+}
+
+// Replace all occurances of a string in a string
+export function replaceAll(input = '', find = '', replace = '') {
+ return input.replace(
+ new RegExp(find.toString().replace(/([.*+?^=!:${}()|[\]/\\])/g, '\\$1'), 'g'),
+ replace.toString(),
+ );
+}
+
+// Convert to title case
+export function toTitleCase(input = '') {
+ return input.toString().replace(/\w\S*/g, text => text.charAt(0).toUpperCase() + text.substr(1).toLowerCase());
+}
+
+// Convert string to pascalCase
+export function toPascalCase(input = '') {
+ let string = input.toString();
+
+ // Convert kebab case
+ string = replaceAll(string, '-', ' ');
+
+ // Convert snake case
+ string = replaceAll(string, '_', ' ');
+
+ // Convert to title case
+ string = toTitleCase(string);
+
+ // Convert to pascal case
+ return replaceAll(string, ' ', '');
+}
+
+// Convert string to pascalCase
+export function toCamelCase(input = '') {
+ let string = input.toString();
+
+ // Convert to pascal case
+ string = toPascalCase(string);
+
+ // Convert first character to lowercase
+ return string.charAt(0).toLowerCase() + string.slice(1);
+}
+
+// Remove HTML from a string
+export function stripHTML(source) {
+ const fragment = document.createDocumentFragment();
+ const element = document.createElement('div');
+ fragment.appendChild(element);
+ element.innerHTML = source;
+ return fragment.firstChild.innerText;
+}
+
+// Like outerHTML, but also works for DocumentFragment
+export function getHTML(element) {
+ const wrapper = document.createElement('div');
+ wrapper.appendChild(element);
+ return wrapper.innerHTML;
+}