@kitql/client
Advanced tools
Comparing version 0.2.0 to 0.2.1
60
index.js
@@ -74,14 +74,22 @@ 'use strict'; | ||
function extractKeyValue(str) { | ||
if (str.startsWith('_$')) { | ||
const [key, value] = str.substring(2).split('('); | ||
if (str.includes('[]$add')) { | ||
const [key, value] = str.split('[]$add'); | ||
return { | ||
key, | ||
value: value.substring(0, value.length - 1) | ||
value: value ? extractKeyValue('_$PROPS' + value).value : -1 | ||
}; | ||
} | ||
else if (str.includes('[]$add')) { | ||
const [key, value] = str.split('[]$add'); | ||
else if (str.includes('[]$remove(')) { | ||
const [key, value] = str.split('[]$remove('); | ||
return { key, value: value.substring(0, value.length - 1) }; | ||
} | ||
else if (str.includes('[]$filter(')) { | ||
const [key, value] = str.split('[]$filter('); | ||
return { key, value: value.substring(0, value.length - 1) }; | ||
} | ||
else if (str.startsWith('_$')) { | ||
const [key, value] = str.substring(2).split('('); | ||
return { | ||
key, | ||
value: value ? extractKeyValue('_$PROPS' + value).value : -1 | ||
value: value.substring(0, value.length - 1) | ||
}; | ||
@@ -114,2 +122,12 @@ } | ||
} | ||
else if (segment.includes('[]$remove')) { | ||
const kvp = extractKeyValue(segment); | ||
const kvpElement = extractKeyValue(kvp.value); | ||
let newArray = obj[kvp.key]; | ||
newArray = newArray.filter((c) => { | ||
return c[kvpElement.key].toString() !== kvpElement.value; | ||
}); | ||
const ToRmvFound = newArray.length !== obj[kvp.key].length; | ||
toReturn = { found: ToRmvFound, obj: { ...obj, [kvp.key]: newArray } }; | ||
} | ||
else if (segment.includes('_$')) { | ||
@@ -135,13 +153,27 @@ const kvp = extractKeyValue(segment); | ||
} | ||
else if (segment.endsWith('[]')) { | ||
let propertyName = segment.substring(0, segment.length - 2); | ||
if (obj[propertyName] !== undefined) { | ||
else if (segment.includes('[]')) { | ||
let [propertyName] = segment.split('[]'); | ||
if (obj && obj[propertyName] !== undefined) { | ||
const kvp = segment.includes('[]$filter(') ? extractKeyValue(segment) : null; | ||
const kvpElement = kvp ? extractKeyValue(kvp.value) : null; | ||
let xPathNext = segments.slice(1).join('.'); | ||
obj[propertyName].forEach((arrayItem, i) => { | ||
const result = objUpdate(toReturn.found, obj[propertyName][i], newData, xPathNext); | ||
obj[propertyName][i] = result.obj; | ||
toReturn.found = result.found; | ||
if (toReturn.found) { | ||
return; | ||
if (kvpElement) { | ||
if (obj[propertyName][i][kvpElement.key].toString() === kvpElement.value) { | ||
const result = objUpdate(toReturn.found, obj[propertyName][i], newData, xPathNext); | ||
obj[propertyName][i] = result.obj; | ||
toReturn.found = result.found; | ||
if (toReturn.found) { | ||
return; | ||
} | ||
} | ||
} | ||
else { | ||
const result = objUpdate(toReturn.found, obj[propertyName][i], newData, xPathNext); | ||
obj[propertyName][i] = result.obj; | ||
toReturn.found = result.found; | ||
if (toReturn.found) { | ||
return; | ||
} | ||
} | ||
}); | ||
@@ -148,0 +180,0 @@ } |
@@ -111,4 +111,4 @@ export declare type ClientSettings = { | ||
} | null): number; | ||
patch<D, V>(operationKey: string, store: RequestResult<D, V>, newData: Object, // To be fragments only? | ||
patch<D, V>(operationKey: string, store: RequestResult<D, V>, newData: Object | null, // To be fragments only? | ||
xPath?: string | null): RequestResult<D, V>; | ||
} |
{ | ||
"name": "@kitql/client", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"dependencies": { | ||
@@ -5,0 +5,0 @@ "@kitql/helper": "0.1.5", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38162
898