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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | 1x 1x 1x 1x 1x 1x 9x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 21x 21x 21x 21x 21x 21x 21x 37x 34x 15x 15x 3x 3x 34x 5x 5x 34x 37x 21x 21x 37x 37x 37x 1x 37x 1x 1x 37x 21x 21x 21x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 1x 1x 3x 1x 1x 3x 3x 1x 1x 2x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 1x | export {}
import { validate } from 'jsonschema'
import { Utils } from '../BaseUtils'
export class DataUtils extends Utils {
constructor() {
super()
}
/**
* @description Compare two objects and return the difference
* @param {object} obj1 - The first object to compare.
* @param {object} obj2 - The second object to compare.
* @param {array} exclude - An array of keys to exclude from the comparison.
* @returns {object|boolean} - Returns the difference between the two objects or false if there is no difference.
*/
compareObjects = (
obj1: Record<string, any>,
obj2: Record<string, any>,
exclude: Array<string> = []
): Record<string, any> | Boolean => {
const diff: any = {}
for (const key in obj1) {
if (obj1.hasOwnProperty(key) && !exclude.includes(key)) {
if (typeof obj1[key] === 'object' && typeof obj2[key] === 'object') {
const nestedDiff = this.compareObjects(obj1[key], obj2[key], exclude)
if (Object.keys(nestedDiff).length > 0) {
diff[key] = nestedDiff
}
} else if (obj1[key] !== obj2[key]) {
diff[key] = { oldValue: obj1[key], newValue: obj2[key] }
}
}
}
for (const key in obj2) {
if (
obj2.hasOwnProperty(key) &&
!obj1.hasOwnProperty(key) &&
!exclude.includes(key)
) {
diff[key] = { oldValue: undefined, newValue: obj2[key] }
}
}
return JSON.stringify(diff) === '{}' ? false : diff
}
/**
* @description Validate JSON against a schema
* @param {string|object} json - The JSON (or object) to validate.
* @param {string|object} schema - The JSON schema (or object) to validate against.
* @returns {boolean|object} - Returns true if valid or an object of issues.
*/
validateJson = (
json: string | object,
schema: string | object
): boolean | string[] => {
try {
if (typeof json === 'string') {
json = JSON.parse(json)
}
if (typeof schema === 'string') {
schema = JSON.parse(schema)
}
const result = validate(json, schema)
if (!result.valid) {
return result.errors.map((e) => e.stack)
}
return true
} catch (error) {
return false
}
}
/**
* @description Converts a string to camel-case
* @param {string} str - The string to be converted
* @returns {string} - Returns string formatted as camel-case
*/
toCamelCase = (str: string): string => {
return str
.toLowerCase()
.replace(/(?:^\w|[A-Z]|\b\w)/g, (ltr, idx) =>
idx === 0 ? ltr.toLowerCase() : ltr.toUpperCase()
)
.replace(/\s+/g, '')
}
/**
* @description Converts a string in camel-case to snake-case
* @param {string} str - The camel-case string to be converted
* @returns {string} - Returns string formatted as snake-case
*/
camelToSnake = (str: string): string => {
return str
.replace(/\W+/g, '-')
.replace(/([a-z\d])([A-Z])/g, '$1-$2')
.toLowerCase()
}
/**
* @description Removes hyphens
* @param {string} str - Source string
* @returns {string} - Returns string without hyphens
*/
hyphenToNull = (str: string): string => {
return str.replace(/-/g, '')
}
/**
* @description Replaces hyphens with spaces
* @param {string} str - Source string
* @returns {string} - Returns string without hyphens
*/
hyphenToSpace = (str: string): string => {
return str.replace(/-/g, ' ')
}
/**
* @description Replaces underscores with spaces
* @param {string} str - Source string
* @returns {string} - Returns string without underscores
*/
underscoreToSpace = (str: string): string => {
return str.replace(/(_)/g, ' ')
}
/**
* @description Replaces spaces with hyphens
* @param {string} str - Source string
* @returns {string} - Returns string with hyphens
*/
spaceToHyphen = (str: string): string => {
return str.replace(/ /g, '-')
}
/**
* @description Changes camel or pascal case to capitalized words
* @param {string} str - Source string
* @returns {string} - Returns converted string
*/
toRegularForm = (str: string): string => {
return str
.replace(/([A-Z])/g, ' $1')
.replace(/^./, (ltr) => ltr.toUpperCase())
}
/**
* @description Prepend prefix to converted snake case string
* @param {string} str - Source string
* @returns {string} - Returns converted string
*/
prefixSnakeCase = (pre: string, str: string): string => {
return `${pre}-${this.camelToSnake(str)}`
}
/**
* @description Parses intials from a "name" string
* @param {string} str - Name string (e.g. "John Hancock", "John", "John Tyler Smith")
* @returns {string} - Returns initials
*/
parseInitials = (str: string): string => {
const parts = str.split(' ')
if (parts.length === 1) return parts[0].substring(0, 2).toUpperCase()
return `${parts[0].substring(0, 1).toUpperCase()}${parts[parts.length - 1].substring(0, 1).toUpperCase()}`
}
/**
* @description Provides ordinal of a number
* @param {number} num - Number to be converted to ordinal
* @param {boolean} isUpperCase - Set to uppercase when true or lowercase when not true
* @returns {string} - Returns ordinal string
*/
numToOrdinal = (num: number, isUpperCase: boolean): string => {
if (typeof num !== 'number') throw Error('num must be a number')
const s = ['th', 'st', 'nd', 'rd']
const v = num % 100
const output = num + (s[(v - 20) % 10] || s[v] || s[0])
return isUpperCase ? output.toUpperCase() : output
}
}
|