Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADDED: String Function: String.str_replace(search, replace, caseSensitive = true): Replace all occurrences of the search string with the replacement string. #112

Merged
merged 1 commit into from
Feb 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 55 additions & 2 deletions src/js/helpers/string.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 SGNetworks. All rights reserved.
* Copyright (c) 2022-2023 SGNetworks. All rights reserved.
*
* The software is an exclusive copyright of "SGNetworks" and is provided as is exclusively with only "USAGE" access. "Modification", "Alteration", "Re-distribution" is completely prohibited.
* VIOLATING THE ABOVE TERMS IS A PUNISHABLE OFFENSE WHICH MAY LEAD TO LEGAL CONSEQUENCES.
Expand All @@ -23,6 +23,59 @@ String.prototype.rtrim = function(s) {
return this.replace(new RegExp(s + "*$"), "");
};

/***
* Replace all occurrences of the search string with the replacement string.
* <br>
* This function returns a <b>string</b> with all occurrences of search in <b><i>subject</i></b> replaced with the given <b><i>replace</i></b> value.
*
* @param {string|array}search The value being searched for, otherwise known as the needle. An array may be used to designate multiple needles.
* @param {string|array}replace The replacement value that replaces found search values. An array may be used to designate multiple replacements.
* @param {boolean}caseSensitive Specify if the searching would be case-sensitive or not.
*
* @returns {string} A <b>string</b> with the replaced values.
*/
String.prototype.str_replace = function(search, replace, caseSensitive = true) {
let v = this;

function getIndicesOf(needle, haystack) {
const searchStrLen = needle.length;
if(searchStrLen === 0) {
return [];
}
let startIndex = 0, index, indices = [];
if(!caseSensitive) {
haystack = haystack.toLowerCase();
needle = needle.toLowerCase();
}
while((index = haystack.indexOf(needle, startIndex)) > -1) {
indices.push(index);
startIndex = index + searchStrLen;
}
return indices;
}

if(typeof search === 'object' && typeof replace === 'object') {
search.forEach((s, i) => {
const r = replace[i];
v = v.replace(s, r);
});
} else if(typeof search === 'object' && (typeof replace === 'string' || typeof replace === 'number')) {
search.forEach(s => v = v.replace(s, replace));
} else if((typeof search === 'string' || typeof search === 'number') && typeof replace === 'object') {
let indices = getIndicesOf(search, v);
indices.forEach((k, i) => {
const s = search[k],
r = replace[i];
v = v.replace(s, r);
});
v = v.replace(search, replace);
} else if((typeof search === 'string' || typeof search === 'number') && (typeof replace === 'string' || typeof replace === 'number')) {
v = v.replace(search, replace);
}

return v;
};

/**
* Convert a string to HTML entities
*/
Expand All @@ -44,4 +97,4 @@ $.expr[":"].textEquals = $.expr.createPseudo(function(arg) {
return function(elem) {
return $(elem).text().trim().match("^" + arg + "$");
};
});
});