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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | 1 1 1 1 3 1 2 1 1714 439 1275 1 2 1 1 2 1 56 40 16 13 3 1 1123 1123 1123 1123 1132 1132 7 1125 92 92 100 100 1033 1123 1 2096 2096 1237 1 2898 2898 859 1237 1237 1237 1237 212 200 212 212 212 212 1237 859 1 80 1 79 4 79 1 589 263 589 589 3 3 589 318 271 271 222 49 271 1 59 42 59 19 19 40 4 36 36 22 14 59 1 110 110 110 4 110 67 67 2 65 110 110 18 18 18 92 26 66 92 110 1 22 2 22 24 20 24 7 22 1 1275 1275 43 1275 1 3185 1 5 5 5 5 5 5 12 12 5 1 1 1 | Iif (typeof window === 'object') { var _ = window._; } else { var _ = require('underscore'); } var isConditionalOperator = /^\$(AND|OR|XOR|NOT|AND\$NOT|OR\$NOT)$/i; var sortStringAndOptionsArguments = function(string, options) { if (typeof string === 'object') { return { string: null, options: string }; } return { string: string || null, options: options || {} } } var sortOptionsAndCallbackArguments = function(options, callback) { if (typeof options === 'function') { return { options: {}, callback: options }; } return { options: options || {}, callback: callback } } var sortStringAndCallbackArguments = function(string, callback) { if (typeof string === 'function') { callback = string; string = null; } return { callback: callback, string: string } } var getIdFromObject = function(o) { if ((typeof o === 'object') && (!isNaN(o._id_))) return o._id_; if (!isNaN(parseInt(o))) return parseInt(o); // else return o.id || null; } // source: https://gist.github.com/penguinboy/762197 var flattenObject = function(ob, keepNullValues) { var toReturn = {}; Eif (typeof keepNullValues !== 'boolean') keepNullValues = true; for (var i in ob) { Iif (!ob.hasOwnProperty(i)) continue; if ((keepNullValues) && (ob[i] === null)) { toReturn[i] = ob[i]; } else if ((typeof ob[i]) === 'object') { var flatObject = flattenObject(ob[i]); for (var x in flatObject) { Iif (!flatObject.hasOwnProperty(x)) continue; toReturn[i + '.' + x] = flatObject[x]; } } else { toReturn[i] = ob[i]; } } return toReturn; }; // source: https://github.com/hughsk/flat/blob/master/index.js var unflattenObject = function (target, opts) { var opts = opts || {} , delimiter = opts.delimiter || '.' , result = {} if (Object.prototype.toString.call(target) !== '[object Object]') { return target } function getkey(key) { var parsedKey = parseInt(key) return (isNaN(parsedKey) ? key : parsedKey) }; Object.keys(target).forEach(function(key) { var split = key.split(delimiter) , firstNibble , secondNibble , recipient = result firstNibble = getkey(split.shift()) secondNibble = getkey(split[0]) while (secondNibble !== undefined) { if (recipient[firstNibble] === undefined) { recipient[firstNibble] = ((typeof secondNibble === 'number') ? [] : {}) } recipient = recipient[firstNibble] Eif (split.length > 0) { firstNibble = getkey(split.shift()) secondNibble = getkey(split[0]) } } // unflatten again for 'messy objects' recipient[firstNibble] = unflattenObject(target[key]) }); return result }; var escapeString = function(s) { if (typeof s !== 'string') return s; // trim quotes if exists if ( (/^".+"$/.test(s)) || (/^'.+'$/.test(s)) ) s = s.substr(1,s.length-2); return s.replace(/^(['"]{1})/, '\\$1').replace(/([^\\]){1}(['"]{1})/g,'$1\\$2'); } var escapeProperty = function(identifier, delimiter) { if (typeof delimiter !== 'string') delimiter = '`'; // do we have s.th. like ?! appending var appending = identifier.match(/^(.*)([\?\!]{1})$/) || ''; // console.log(appending) if ((appending)&&(appending[2])) { identifier = appending[1]; appending = appending[2]; } // no escaping if last char is a delimiter or ?, because we expect that the identifier is already escaped somehow if (new RegExp(''+delimiter+'{1}$').test(identifier)) return identifier; // remove all delimiters ` identifier = identifier.replace(new RegExp(delimiter, 'g'), ''); if (/^(.+?)\..+$/.test(identifier)) identifier = identifier.replace(/^(.+?)\.(.+)$/, '$1.'+delimiter+'$2'+delimiter); else identifier = delimiter+identifier+delimiter; return identifier + appending; } var valueToStringForCypherQuery = function(value, delimiter) { if (typeof delimiter !== 'string') delimiter = ''; if ((value) && (value.constructor === RegExp)) { value = value.toString().replace(/^\/(\^)*(.+?)\/[ig]*$/, (value.ignoreCase) ? '$1(?i)$2' : '$1$2'); // replace `\` with `\\` to keep compatibility with Java regex value = value.replace(/([^\\]{1})\\([^\\]{1})/g, '$1\\\\$2'); } else { if ((typeof value === 'undefined') || (value === null)) value = 'NULL'; else Iif (value === NaN) // what would be a good representation of NaN? value = delimiter+'NaN'+delimiter; else if ((typeof value === 'boolean') || (typeof value === 'number')) value = String(value); else value = delimiter + escapeString(value) + delimiter; } return value; } var cypherKeyValueToString = function(key, originalValue, identifier, conditionalParametersObject) { var value = originalValue; var s = ''; // string that will be returned if (typeof conditionalParametersObject !== 'object') conditionalParametersObject = null; if (typeof identifier === 'string') { Iif (/^[nmr]\./.test(key)) // we have already an identifier key = key; else if (/[\?\!]$/.test(key)) // we have a default statement, escape without ! or ? key = identifier+'.'+key; else key = identifier+'.'+key; } key = escapeProperty(key) if (_.isRegExp(value)) { value = valueToStringForCypherQuery(value); value = ((conditionalParametersObject) && (conditionalParametersObject.valuesToParameters)) ? ((conditionalParametersObject.addValue) ? conditionalParametersObject.addValue(value) : value) : "'"+value+"'"; s = key + " =~ " + value; } else { // convert to string if ((_.isNumber(value)) || (_.isBoolean(value))) value = ((conditionalParametersObject) && (conditionalParametersObject.valuesToParameters)) ? ((conditionalParametersObject.addValue) ? conditionalParametersObject.addValue(value) : value) : valueToStringForCypherQuery(value); // else escape else value = ((conditionalParametersObject) && (conditionalParametersObject.valuesToParameters)) ? ((conditionalParametersObject.addValue) ? conditionalParametersObject.addValue(value) : value) : "'"+escapeString(value)+"'"; s = key + " = " + value; } return s; } var extractAttributesFromCondition = function(condition, attributes) { if (typeof attributes === 'undefined') attributes = []; _.each(condition, function(value, key) { if (_.isObject(value)) { extractAttributesFromCondition(condition[key], attributes); } if ( (!isConditionalOperator.test(key)) && (/^[a-zA-Z\_\-\.]+$/.test(key)) ) { // remove identifiers if exists attributes.push(key.replace(/^[nmr]{1}\./,'')); } }); return _.uniq(attributes); } var constructorNameOfFunction = function(func) { var name = func.constructor.toString().match(/^function\s(.+?)\(/)[1]; if (name === 'Function') { name = func.toString().match(/^function\s(.+)\(/)[1] } return name; } var isObjectLiteral = function(data) { return Boolean((typeof data === 'object') && (data !== null) && (typeof Object.keys(data).length === 'number')); } var serializeObjectForCypher = function(o, options) { o = this.flattenObject(o); var result = []; Eif (typeof options !== 'object') options = {}; options = _.defaults(options, { identifierDelimiter: '`', valueDelimiter: "'", }); for (var attr in o) { var value = o[attr]; result.push(escapeProperty(attr)+' : '+valueToStringForCypherQuery(value, options.valueDelimiter)); } return '{ '+result.join(', ')+' }'; } var helpers = { sortStringAndOptionsArguments: sortStringAndOptionsArguments, sortOptionsAndCallbackArguments: sortOptionsAndCallbackArguments, sortStringAndCallbackArguments: sortStringAndCallbackArguments, flattenObject: flattenObject, unflattenObject: unflattenObject, extractAttributesFromCondition: extractAttributesFromCondition, getIdFromObject: getIdFromObject, escapeString: escapeString, escapeProperty: escapeProperty, constructorNameOfFunction: constructorNameOfFunction, cypherKeyValueToString: cypherKeyValueToString, valueToStringForCypherQuery: valueToStringForCypherQuery, md5: (typeof window === 'object') ? window.Neo4jMapper.md5 : require('./lib/md5'), isConditionalOperator: isConditionalOperator, isObjectLiteral: isObjectLiteral, serializeObjectForCypher: serializeObjectForCypher, }; Eif (typeof window !== 'object') { module.exports = exports = helpers; } else { window.Neo4jMapper.helpers = helpers; } |