'use strict'; module.exports = { copy: copy, deepClone: deepClone, checkDataType: checkDataType, checkDataTypes: checkDataTypes, toHash: toHash, getProperty: getProperty, escapeQuotes: escapeQuotes, ucs2length: ucs2length, varOccurences: varOccurences, varReplace: varReplace, cleanUpCode: cleanUpCode, cleanUpVarErrors: cleanUpVarErrors, schemaHasRules: schemaHasRules, stableStringify: require('json-stable-stringify'), toQuotedString: toQuotedString, getPathExpr: getPathExpr, getPath: getPath, unescapeFragment: unescapeFragment, escapeFragment: escapeFragment }; function copy(o, to) { to = to || {}; for (var key in o) to[key] = o[key]; return to; } function deepClone(o) { if (typeof o != 'object' || o === null) return o; var res; if (Array.isArray(o)) { res = []; for (var i=0; i= 0xD800 && value <= 0xDBFF && pos < len) { // high surrogate, and there is a next character value = str.charCodeAt(pos); if ((value & 0xFC00) == 0xDC00) pos++; // low surrogate } } return length; } function varOccurences(str, dataVar) { dataVar += '[^0-9]'; var matches = str.match(new RegExp(dataVar, 'g')); return matches ? matches.length : 0; } function varReplace(str, dataVar, expr) { dataVar += '([^0-9])'; expr = expr.replace(/\$/g, '$$$$'); return str.replace(new RegExp(dataVar, 'g'), expr + '$1'); } var EMPTY_ELSE = /else\s*{\s*}/g , EMPTY_IF_NO_ELSE = /if\s*\([^)]+\)\s*\{\s*\}(?!\s*else)/g , EMPTY_IF_WITH_ELSE = /if\s*\(([^)]+)\)\s*\{\s*\}\s*else(?!\s*if)/g; function cleanUpCode(out) { return out.replace(EMPTY_ELSE, '') .replace(EMPTY_IF_NO_ELSE, '') .replace(EMPTY_IF_WITH_ELSE, 'if (!($1))'); } var ERRORS_REGEXP = /[^v\.]errors/g , REMOVE_ERRORS = /var errors = 0;|var vErrors = null;|validate.errors = vErrors;/g , RETURN_VALID = 'return errors === 0;' , RETURN_TRUE = 'validate.errors = null; return true;'; function cleanUpVarErrors(out) { var matches = out.match(ERRORS_REGEXP); if (matches && matches.length === 2) return out.replace(REMOVE_ERRORS, '') .replace(RETURN_VALID, RETURN_TRUE); else return out; } function schemaHasRules(schema, rules) { for (var key in schema) if (rules[key]) return true; } function toQuotedString (str) { return '\'' + escapeQuotes(str) + '\''; } function getPathExpr (currentPath, expr, jsonPointers, isNumber) { var path = jsonPointers // false by default ? '\'/\' + ' + expr + (isNumber ? '' : '.replace(/~/g, \'~0\').replace(/\\//g, \'~1\')') : (isNumber ? '\'[\' + ' + expr + ' + \']\'' : '\'[\\\'\' + ' + expr + ' + \'\\\']\''); return joinPaths(currentPath, path); } function getPath (currentPath, prop, jsonPointers) { var path = jsonPointers // false by default ? toQuotedString('/' + escapeFragment(prop)) : toQuotedString(getProperty(prop)); return joinPaths(currentPath, path); } function joinPaths (a, b) { if (a == '""') return b; return (a + ' + ' + b).replace(/' \+ '/g, ''); } function unescapeFragment(str) { return decodeURIComponent(str) .replace(/~1/g, '/') .replace(/~0/g, '~'); } function escapeFragment(str) { str = str.replace(/~/g, '~0').replace(/\//g, '~1'); return encodeURIComponent(str); }