unomi-sdk-node
Version:
Node module to interact with unomi.
147 lines (146 loc) • 4.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isItemInList = exports.flattenObject = exports.getProfileSegmentMatchTypes = exports.getProfilePropertyComparisonOperators = exports.allSameType = exports.areArraysEqual = exports.findAllIndicesOfCharacter = exports.generateUuid = void 0;
/**
* @function createUuid
*/
function generateUuid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
let r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
exports.generateUuid = generateUuid;
/**
* @function findAllIndicesOfCharacter
* @param {string} value
* @param {string} character
*/
function findAllIndicesOfCharacter(value, character) {
let indices = new Array; // result array
for (let i = 0; i < value.length; i++) { // go through string
if (value[i] === character) { // check if same character
indices.push(i); // add index to array
}
}
return indices; // eturn result array
}
exports.findAllIndicesOfCharacter = findAllIndicesOfCharacter;
/**
* @function areArraysEqual
* @param {Array<number>} arr1
* @param {Array<number>} arr2
*/
function areArraysEqual(arr1, arr2) {
if (arr1.length !== arr2.length)
return false; // check if the arrays are the same length
// check if all items exist and are in the same order
for (var i = 0; i < arr1.length; i++) {
if (arr1[i] !== arr2[i])
return false;
}
return true; // otherwise, return true
}
exports.areArraysEqual = areArraysEqual;
/**
* @function allSameType
* @param {Array<any>} arr
*/
function allSameType(arr) {
// note for function: https://stackoverflow.com/questions/49411862/check-if-all-values-of-array-are-of-the-same-type
return new Set(arr.map(x => typeof x)).size <= 1; // return true or false
}
exports.allSameType = allSameType;
/**
* @function getProfilePropertyComparisonOperators
*/
function getProfilePropertyComparisonOperators() {
const operators = [
"equals",
"notEquals",
"greaterThan",
"greaterThanOrEqualTo",
"lessThan",
"lessThanOrEqualTo",
"between",
"exists",
"missing",
"contains",
"notContains",
"startsWith",
"endsWith",
// "matchesRegex",
"in",
"notIn",
"all",
// "inContains",
"hasSomeOf",
"hasNoneOf",
];
return operators; // return comparison operators
}
exports.getProfilePropertyComparisonOperators = getProfilePropertyComparisonOperators;
/**
* @function getProfileSegmentComparisonOperators
*/
function getProfileSegmentMatchTypes() {
const matchTypes = [
"in",
"notIn",
"all"
];
return matchTypes; // return match types
}
exports.getProfileSegmentMatchTypes = getProfileSegmentMatchTypes;
/**
* @function flattenObject
* @param {any} ob
*/
function flattenObject(ob) {
var toReturn = {}; // result object
for (var i in ob) { // go through object
if (!ob.hasOwnProperty(i))
continue; //
if ((typeof ob[i]) == 'object') { // check if property is an object
if (i != 'fields') { // check if object key is not "fields"
var a = ob[i]; //
if (ob[i].hasOwnProperty('properties')) { //
a = ob[i]['properties']; // assign value to a
}
var flatObject = flattenObject(a); //
for (var x in flatObject) { // go through object
if (!flatObject.hasOwnProperty(x))
continue; //
if (x != 'type') { // check if object key is not "type"
toReturn[i + '.' + x] = flatObject[x]; //
}
else { // object key is "type"
toReturn[i] = flatObject[x]; //
}
}
}
}
else { // property is not an object
toReturn[i] = ob[i]; // add property to result object
}
}
return toReturn; // return result object
}
exports.flattenObject = flattenObject;
;
/**
* @function isItemInList
* @param {any} listItems
* @param {any} item
*/
function isItemInList(listItems, item) {
let response = false; // false by default
for (let index in listItems) { // go through each element in list
if (item.includes(listItems[index])) { // check if item contains an element from list
response = true;
}
}
return response;
}
exports.isItemInList = isItemInList;
;