aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/utils/strings.js
diff options
context:
space:
mode:
authorSam Potts <sam@potts.es>2018-06-18 21:41:25 +1000
committerSam Potts <sam@potts.es>2018-06-18 21:41:25 +1000
commitcc3c0b54484e6f5a7b4dba8a36a44f345e462f26 (patch)
tree5fe2838546d9f981b21572dee88ee7a1c3195477 /src/js/utils/strings.js
parent4811e3333f1417bc9e14f6cc38afc789e9051c4c (diff)
parent3c9c1b4cdcd0eb9076c3f0bafbabb057ee140c42 (diff)
downloadplyr-cc3c0b54484e6f5a7b4dba8a36a44f345e462f26.tar.lz
plyr-cc3c0b54484e6f5a7b4dba8a36a44f345e462f26.tar.xz
plyr-cc3c0b54484e6f5a7b4dba8a36a44f345e462f26.zip
Merge branch 'develop'
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..c872498c
--- /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;
+}