verified
Miscellaneous helpersCollection of useful code snippets (helpers) in SSJS (server-side JavaScript).
Table of contents
inArray
Does a value exist in an array.
function inArray(arr, k) {
var out = false,
len = arr.length;
while(len--) {
if (arr[len] == k) out = true;
}
return out;
}
<script runat="server">
Platform.Load("core", "1");
try {
var fruit = ["Apple", "Kiwi", "Tomato"];
var result = inArray(fruit, "Tomato");
Write(Stringify(result));
} catch(error) {
Write(Stringify(error));
}
function inArray(arr, k) {
var out = false,
len = arr.length;
while(len--) {
if (arr[len] == k) out = true;
}
return out;
}
</script>
true
isEmpty
Is the String empty.
function isEmpty(str) {
return (str == null || str == undefined || str.length == 0)
}
<script runat="server">
Platform.Load("core", "1");
try {
var str = "";
var result = isEmpty(str);
Write(Stringify(result));
} catch(error) {
Write(Stringify(error));
}
function isEmpty(str) {
return (str == null || str == undefined || str.length == 0)
}
</script>
true
isEmptyObject
Is the object empty.
function isEmptyObject(obj) {
if(obj === null) return true;
for(var prop in obj) {
if(Object.prototype.hasOwnProperty.call(obj, prop)) {
return false;
}
}
return JSON.stringify(obj) === JSON.stringify({});
}
<script runat="server">
Platform.Load("core", "1");
try {
var o = {
name: "Demo",
props: {
age: 34
}
};
var result = isEmptyObject(o.props);
Write(Stringify(result));
} catch(error) {
Write(Stringify(error));
}
function isEmptyObject(obj) {
if(obj === null) return true;
for(var prop in obj) {
if(Object.prototype.hasOwnProperty.call(obj, prop)) {
return false;
}
}
return JSON.stringify(obj) === JSON.stringify({});
}
</script>
false
capitalizeFirstLetter
Set the first letter of a string of characters as a capital letter.
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
<script runat="server">
Platform.Load("core", "1");
try {
var str = "the quick brown fox jumps over the lazy dog.";
var result = capitalizeFirstLetter(str);
Write(Stringify(result));
} catch(error) {
Write(Stringify(error));
}
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
</script>
The quick brown fox jumps over the lazy dog.
sanitizeString
Remove extra spaces, carriage returns and line breaks.
function sanitizeString(str) {
var no_spaces = String(str).replace(/\s+/g, ' ');
var no_breaks = String(no_spaces).replace(/(\r\n|\n|\r)/gm, '');
return no_breaks;
}
<script runat="server">
Platform.Load("core", "1");
try {
var str = "The quick brown fox\n\r jumps over the lazy dog.\n\r";
var result = sanitizeString(str);
Write(Stringify(result));
} catch(error) {
Write(Stringify(error));
}
function sanitizeString(str) {
var no_spaces = String(str).replace(/\s+/g, ' ');
var no_breaks = String(no_spaces).replace(/(\r\n|\n|\r)/gm, '');
return no_breaks;
}
</script>
The quick brown fox jumps over the lazy dog.
snakecaseString
Replace special characters and spaces by an underscore.
function snakecaseString(str) {
var res = str.replace(/[^A-Z0-9]+/ig, "_");
res = res.replace(/_+/g, '_');
return (res.lastIndexOf("_") == res.length - 1) ? res.substring(0, res.length - 1) : res;
}
<script runat="server">
Platform.Load("core", "1");
try {
var str = "Hello world | A new beginning! ^_^";
var result = snakecaseString(str);
Write(Stringify(result));
} catch(error) {
Write(Stringify(error));
}
function snakecaseString(str) {
var res = str.replace(/[^A-Z0-9]+/ig, "_");
res = res.replace(/_+/g, '_');
return (res.lastIndexOf("_") == res.length - 1) ? res.substring(0, res.length - 1) : res;
}
</script>
Hello_world_A_new_beginning
chunkString
Split a String into chunks of a predefined length.
function chunkString(str, len) {
return str.match(new RegExp('[\\s\\S]{1,' + len + '}', 'g'));
}
<script runat="server">
Platform.Load("core", "1");
try {
var str = "the quick brown fox jumps over the lazy dog.";
var result = chunkString(str, 20);
Write(Stringify(result));
} catch(error) {
Write(Stringify(error));
}
function chunkString(str, len) {
return str.match(new RegExp('[\\s\\S]{1,' + len + '}', 'g'));
}
</script>
[
"the quick brown fox ",
"jumps over the lazy ",
"dog."
]