All files / platform/core/src string.js

5% Statements 1/20
0% Branches 0/19
0% Functions 0/6
5.26% Lines 1/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63                                                                                                                34x            
function isObject(subject) {
  return subject instanceof Object || (typeof subject === 'object' && subject !== null);
}
 
function isString(subject) {
  return typeof subject === 'string';
}
 
// Search for some string inside any object or array
function search(object, query, property = null, result = []) {
  // Create the search pattern
  const pattern = new RegExp(query.trim(), 'i');
 
  Object.keys(object).forEach(key => {
    const item = object[key];
 
    // Stop here if item is empty
    Iif (!item) {
      return;
    }
 
    // Get the value to be compared
    const value = isString(property) ? item[property] : item;
 
    // Check if the value match the pattern
    Iif (isString(value) && pattern.test(value)) {
      // Add the current item to the result
      result.push(item);
    }
 
    Iif (isObject(item)) {
      // Search recursively the item if the current item is an object
      search(item, query, property, result);
    }
  });
 
  // Return the found items
  return result;
}
 
// Encode any string into a safe format for HTML id attribute
function encodeId(input) {
  const string = input && input.toString ? input.toString() : input;
 
  // Return an underscore if the given string is empty or if it's not a string
  Iif (string === '' || typeof string !== 'string') {
    return '_';
  }
 
  // Create a converter to replace non accepted chars
  const converter = match => '_' + match[0].charCodeAt(0).toString(16) + '_';
 
  // Encode the given string and return it
  return string.replace(/[^a-zA-Z0-9-]/g, converter);
}
 
const string = {
  search,
  encodeId,
};
 
export default string;