can-query-logic
Advanced tools
Comparing version 0.8.11 to 1.0.0
{ | ||
"name": "can-query-logic", | ||
"version": "0.8.11", | ||
"version": "1.0.0", | ||
"description": "query data", | ||
@@ -5,0 +5,0 @@ "homepage": "", |
@@ -20,17 +20,17 @@ var canSymbol = require("can-symbol"); | ||
function getSchemaProperties(value) { | ||
var constructor = value.constructor; | ||
if(constructor && constructor[schemaSymbol]) { | ||
var schema = constructor[schemaSymbol](); | ||
return schema.keys || {}; | ||
} else { | ||
return {}; | ||
} | ||
var constructor = value.constructor; | ||
if (constructor && constructor[schemaSymbol]) { | ||
var schema = constructor[schemaSymbol](); | ||
return schema.keys || {}; | ||
} else { | ||
return {}; | ||
} | ||
} | ||
function hydrateFilter(values, schemaProperties, hydrateUnknown) { | ||
if(values && typeof values === "object" && ("$or" in values)) { | ||
return hydrateOrs(values.$or, schemaProperties, hydrateUnknown); | ||
} else { | ||
return hydrateAndValues(values, schemaProperties, hydrateUnknown); | ||
} | ||
if (values && typeof values === "object" && ("$or" in values)) { | ||
return hydrateOrs(values.$or, schemaProperties, hydrateUnknown); | ||
} else { | ||
return hydrateAndValues(values, schemaProperties, hydrateUnknown); | ||
} | ||
} | ||
@@ -41,86 +41,86 @@ | ||
// This is used to hydrate a value directly within a `filter`'s And. | ||
function hydrateAndValue(value, prop, SchemaType, hydrateChild){ | ||
// The `SchemaType` is the type of value on `instances` of | ||
// the schema. `Instances` values are different from `Set` values. | ||
if(SchemaType) { | ||
// If there's a `SetType`, we will use that | ||
var SetType = SchemaType[setTypeSymbol]; | ||
if(SetType) { | ||
/// If it exposes a hydrate, this means it can use the current hydrator to | ||
// hydrate its children. | ||
// I'm not sure why it's not taking the `unknown` hydrator instead. | ||
if(SetType.hydrate) { | ||
return SetType.hydrate(value, comparisonsConverter.hydrate); | ||
} | ||
// If the SetType implemented `union`, `intersection`, `difference` | ||
// We can create instances of it directly. | ||
else if(set.hasComparisons(SetType)) { | ||
// Todo ... canReflect.new | ||
return new SetType(value); | ||
} | ||
// If the SetType did not implement the comparison methods, | ||
// it's probably just a "Value" comparison type. We will hydrate | ||
// as a comparison converter, but create an instance of this `"Value"` | ||
// comparison type within the comparison converter. | ||
else { | ||
// inner types | ||
return comparisonsConverter.hydrate(value, function(value){ | ||
return new SetType(value); | ||
}); | ||
} | ||
function hydrateAndValue(value, prop, SchemaType, hydrateChild) { | ||
// The `SchemaType` is the type of value on `instances` of | ||
// the schema. `Instances` values are different from `Set` values. | ||
if (SchemaType) { | ||
// If there's a `SetType`, we will use that | ||
var SetType = SchemaType[setTypeSymbol]; | ||
if (SetType) { | ||
/// If it exposes a hydrate, this means it can use the current hydrator to | ||
// hydrate its children. | ||
// I'm not sure why it's not taking the `unknown` hydrator instead. | ||
if (SetType.hydrate) { | ||
return SetType.hydrate(value, comparisonsConverter.hydrate); | ||
} | ||
// If the SetType implemented `union`, `intersection`, `difference` | ||
// We can create instances of it directly. | ||
else if (set.hasComparisons(SetType)) { | ||
// Todo ... canReflect.new | ||
return new SetType(value); | ||
} | ||
// If the SetType did not implement the comparison methods, | ||
// it's probably just a "Value" comparison type. We will hydrate | ||
// as a comparison converter, but create an instance of this `"Value"` | ||
// comparison type within the comparison converter. | ||
else { | ||
// inner types | ||
return comparisonsConverter.hydrate(value, function(value) { | ||
return new SetType(value); | ||
}); | ||
} | ||
} else { | ||
// There is a `SchemaType`, but it doesn't have a `SetType`. | ||
// Can we create the SetType from the `SchemaType`? | ||
if(makeEnum.canMakeEnumSetType(SchemaType)) { | ||
if(!setTypeMap.has(SchemaType)) { | ||
setTypeMap.set(SchemaType, makeEnum.makeEnumSetType(SchemaType) ); | ||
} | ||
SetType = setTypeMap.get(SchemaType); | ||
return new SetType(value); | ||
} | ||
// It could also have a `ComparisonSetType` which are the values | ||
// within the Maybe type. | ||
else if(makeMaybe.canMakeMaybeSetType(SchemaType)) { | ||
if(!setTypeMap.has(SchemaType)) { | ||
setTypeMap.set(SchemaType, makeMaybe.makeMaybeSetTypes(SchemaType) ); | ||
} | ||
SetType = setTypeMap.get(SchemaType).Maybe; | ||
return SetType.hydrate(value, comparisonsConverter.hydrate); | ||
} | ||
// We can't create the `SetType`, so lets hydrate with the default behavior. | ||
else { | ||
return comparisonsConverter.hydrate(value, hydrateChild); | ||
} | ||
} | ||
} else { | ||
// HERE {$gt: 1} -> new is.GreaterThan(1) | ||
return comparisonsConverter.hydrate(value, hydrateChild); | ||
} | ||
} else { | ||
// There is a `SchemaType`, but it doesn't have a `SetType`. | ||
// Can we create the SetType from the `SchemaType`? | ||
if (makeEnum.canMakeEnumSetType(SchemaType)) { | ||
if (!setTypeMap.has(SchemaType)) { | ||
setTypeMap.set(SchemaType, makeEnum.makeEnumSetType(SchemaType)); | ||
} | ||
SetType = setTypeMap.get(SchemaType); | ||
return new SetType(value); | ||
} | ||
// It could also have a `ComparisonSetType` which are the values | ||
// within the Maybe type. | ||
else if (makeMaybe.canMakeMaybeSetType(SchemaType)) { | ||
if (!setTypeMap.has(SchemaType)) { | ||
setTypeMap.set(SchemaType, makeMaybe.makeMaybeSetTypes(SchemaType)); | ||
} | ||
SetType = setTypeMap.get(SchemaType).Maybe; | ||
return SetType.hydrate(value, comparisonsConverter.hydrate); | ||
} | ||
// We can't create the `SetType`, so lets hydrate with the default behavior. | ||
else { | ||
return comparisonsConverter.hydrate(value, hydrateChild); | ||
} | ||
} | ||
} else { | ||
// HERE {$gt: 1} -> new is.GreaterThan(1) | ||
return comparisonsConverter.hydrate(value, hydrateChild); | ||
} | ||
} | ||
function hydrateAndValues(values, schemaProperties, hydrateUnknown) { | ||
schemaProperties = schemaProperties || {}; | ||
schemaProperties = schemaProperties || {}; | ||
function hydrateChild(value) { | ||
if(value) { | ||
if(Array.isArray(value)) { | ||
return value.map(hydrateUnknown); | ||
} else if(canReflect.isPlainObject(value)) { | ||
// lets try to get the schema ... | ||
return hydrateAndValues(value, getSchemaProperties(value)); | ||
} | ||
} | ||
if(hydrateUnknown) { | ||
return hydrateUnknown(value); | ||
} else { | ||
return value; | ||
} | ||
} | ||
var clone = {}; | ||
canReflect.eachKey(values, function(value, prop){ | ||
clone[prop] = hydrateAndValue(value, prop, schemaProperties[prop], hydrateChild); | ||
}); | ||
function hydrateChild(value) { | ||
if (value) { | ||
if (Array.isArray(value)) { | ||
return value.map(hydrateUnknown); | ||
} else if (canReflect.isPlainObject(value)) { | ||
// lets try to get the schema ... | ||
return hydrateAndValues(value, getSchemaProperties(value)); | ||
} | ||
} | ||
if (hydrateUnknown) { | ||
return hydrateUnknown(value); | ||
} else { | ||
return value; | ||
} | ||
} | ||
var clone = {}; | ||
canReflect.eachKey(values, function(value, prop) { | ||
clone[prop] = hydrateAndValue(value, prop, schemaProperties[prop], hydrateChild); | ||
}); | ||
return new BasicQuery.KeysAnd(clone); | ||
return new BasicQuery.KeysAnd(clone); | ||
@@ -131,68 +131,68 @@ } | ||
function combineAnds(ands) { | ||
var firstKeys = Object.keys(ands[0].values); | ||
var keys = {}; | ||
var firstKeys = Object.keys(ands[0].values); | ||
var keys = {}; | ||
var keysCompare = new is.In(firstKeys); | ||
var keysCompare = new is.In(firstKeys); | ||
firstKeys.map(function(key){ | ||
keys[key] = []; | ||
}); | ||
firstKeys.map(function(key) { | ||
keys[key] = []; | ||
}); | ||
var sameKeys = ands.every(function(and){ | ||
// have to have the same keys | ||
if(!set.isEqual(keysCompare, new is.In(Object.keys(and.values))) ) { | ||
return false; | ||
} | ||
canReflect.eachKey(and.values, function(value, key){ | ||
keys[key].push(value); | ||
}); | ||
return true; | ||
}); | ||
if(!sameKeys) { | ||
return; | ||
} | ||
// now try to union everything and see if it simplifies ... | ||
var unequalKeys = []; | ||
firstKeys.forEach(function(key){ | ||
var isEqual = keys[key].reduce(function(newSet, lastSetOrFalse){ | ||
if(lastSetOrFalse === false) { | ||
return false; | ||
} | ||
if(lastSetOrFalse === undefined) { | ||
return newSet; | ||
} | ||
var res = set.isEqual(newSet,lastSetOrFalse); | ||
return res ? newSet : false; | ||
}); | ||
if(!isEqual) { | ||
unequalKeys.push(key); | ||
} | ||
}); | ||
var sameKeys = ands.every(function(and) { | ||
// have to have the same keys | ||
if (!set.isEqual(keysCompare, new is.In(Object.keys(and.values)))) { | ||
return false; | ||
} | ||
canReflect.eachKey(and.values, function(value, key) { | ||
keys[key].push(value); | ||
}); | ||
return true; | ||
}); | ||
if (!sameKeys) { | ||
return; | ||
} | ||
// now try to union everything and see if it simplifies ... | ||
var unequalKeys = []; | ||
firstKeys.forEach(function(key) { | ||
var isEqual = keys[key].reduce(function(newSet, lastSetOrFalse) { | ||
if (lastSetOrFalse === false) { | ||
return false; | ||
} | ||
if (lastSetOrFalse === undefined) { | ||
return newSet; | ||
} | ||
var res = set.isEqual(newSet, lastSetOrFalse); | ||
return res ? newSet : false; | ||
}); | ||
if (!isEqual) { | ||
unequalKeys.push(key); | ||
} | ||
}); | ||
if(unequalKeys.length !== 1) { | ||
return; | ||
} | ||
var unionKey = unequalKeys[0]; | ||
// lets see if we can union that one value | ||
var unioned = keys[unionKey].reduce(function(cur, last){ | ||
return set.union(cur, last); | ||
}, set.EMPTY); | ||
if (unequalKeys.length !== 1) { | ||
return; | ||
} | ||
var unionKey = unequalKeys[0]; | ||
// lets see if we can union that one value | ||
var unioned = keys[unionKey].reduce(function(cur, last) { | ||
return set.union(cur, last); | ||
}, set.EMPTY); | ||
var result = {}; | ||
firstKeys.map(function(key){ | ||
result[key] = keys[key][0]; | ||
}); | ||
result[unionKey] = unioned; | ||
return new BasicQuery.KeysAnd(result); | ||
var result = {}; | ||
firstKeys.map(function(key) { | ||
result[key] = keys[key][0]; | ||
}); | ||
result[unionKey] = unioned; | ||
return new BasicQuery.KeysAnd(result); | ||
} | ||
function hydrateOrs(values, schemaProperties, hydrateUnknown ) { | ||
var comparisons = values.map(function(value){ | ||
return hydrateAndValues(value, schemaProperties, hydrateUnknown); | ||
}); | ||
var combined = combineAnds(comparisons); | ||
if(combined) { | ||
return combined; | ||
} | ||
return new BasicQuery.Or(comparisons); | ||
function hydrateOrs(values, schemaProperties, hydrateUnknown) { | ||
var comparisons = values.map(function(value) { | ||
return hydrateAndValues(value, schemaProperties, hydrateUnknown); | ||
}); | ||
var combined = combineAnds(comparisons); | ||
if (combined) { | ||
return combined; | ||
} | ||
return new BasicQuery.Or(comparisons); | ||
} | ||
@@ -204,116 +204,121 @@ | ||
var id = schema.identity && schema.identity[0]; | ||
var keys = schema.keys; | ||
var id = schema.identity && schema.identity[0]; | ||
var keys = schema.keys; | ||
var serializeMap = [ | ||
[BasicQuery.Or, function(or, serializer){ | ||
return or.values.map(function(value){ | ||
return serializer(value); | ||
}); | ||
}], | ||
// this destructures ANDs with OR-like clauses | ||
[BasicQuery.KeysAnd, function(and, serializer){ | ||
var ors = []; | ||
var result = {}; | ||
canReflect.eachKey(and.values, function(value, key){ | ||
// is value universal ... if not, we don't need to add anything | ||
var serializeMap = [ | ||
[BasicQuery.Or, function(or, serializer) { | ||
return or.values.map(function(value) { | ||
return serializer(value); | ||
}); | ||
}], | ||
// this destructures ANDs with OR-like clauses | ||
[BasicQuery.KeysAnd, function(and, serializer) { | ||
var ors = []; | ||
var result = {}; | ||
canReflect.eachKey(and.values, function(value, key) { | ||
// is value universal ... if not, we don't need to add anything | ||
if(typeof value.orValues === "function") { | ||
canReflect.addValues( ors, value.orValues().map(function(orValue){ | ||
var result = {}; | ||
result[key] = serializer(orValue); | ||
return result; | ||
}) ); | ||
} else { | ||
result[key] = serializer(value); | ||
} | ||
}); | ||
if(ors.length) { | ||
if(ors.length === 1 ) { | ||
return ors[0]; | ||
} else { | ||
return { | ||
$or: ors.map(function(orPart){ | ||
return canReflect.assign( canReflect.serialize(result), orPart); | ||
}) | ||
}; | ||
} | ||
} else { | ||
return result; | ||
} | ||
if (typeof value.orValues === "function") { | ||
canReflect.addValues(ors, value.orValues().map(function(orValue) { | ||
var result = {}; | ||
result[key] = serializer(orValue); | ||
return result; | ||
})); | ||
} else { | ||
result[key] = serializer(value); | ||
} | ||
}); | ||
if (ors.length) { | ||
if (ors.length === 1) { | ||
return ors[0]; | ||
} else { | ||
return { | ||
$or: ors.map(function(orPart) { | ||
return canReflect.assign(canReflect.serialize(result), orPart); | ||
}) | ||
}; | ||
} | ||
} else { | ||
return result; | ||
} | ||
}], | ||
[BasicQuery.RecordRange, function(range){ | ||
return {start: range.start, end: range.end}; | ||
}], | ||
[BasicQuery, function(basicQuery, childSerializer){ | ||
}], | ||
[BasicQuery.RecordRange, function(range) { | ||
return { | ||
start: range.start, | ||
end: range.end | ||
}; | ||
}], | ||
[BasicQuery, function(basicQuery, childSerializer) { | ||
var filter = set.isEqual(basicQuery.filter, set.UNIVERSAL) ? {} : childSerializer(basicQuery.filter); | ||
var filter = set.isEqual(basicQuery.filter, set.UNIVERSAL) ? {} : childSerializer(basicQuery.filter); | ||
var res = {}; | ||
if(canReflect.size(filter) !== 0) { | ||
res.filter= filter; | ||
} | ||
var res = {}; | ||
if (canReflect.size(filter) !== 0) { | ||
res.filter = filter; | ||
} | ||
if(!set.isEqual(basicQuery.page, defaultQuery.page)) { | ||
// we always provide the start, even if it's 0 | ||
res.page = { | ||
start: basicQuery.page.start | ||
}; | ||
if(basicQuery.page.end !== defaultQuery.page.end) { | ||
res.page.end = basicQuery.page.end; | ||
} | ||
} | ||
if (!set.isEqual(basicQuery.page, defaultQuery.page)) { | ||
// we always provide the start, even if it's 0 | ||
res.page = { | ||
start: basicQuery.page.start | ||
}; | ||
if (basicQuery.page.end !== defaultQuery.page.end) { | ||
res.page.end = basicQuery.page.end; | ||
} | ||
} | ||
if(basicQuery.sort.key !== id) { | ||
res.sort = basicQuery.sort.key; | ||
} | ||
return res; | ||
if (basicQuery.sort.key !== id) { | ||
res.sort = basicQuery.sort.key; | ||
} | ||
return res; | ||
}] | ||
]; | ||
}] | ||
]; | ||
// Makes a sort type that can make a compare function using the SetType | ||
var Sort = BasicQuery.makeSort(keys, hydrateAndValue); | ||
var serializer = new Serializer(serializeMap); | ||
serializer.add(comparisonsConverter.serializer); | ||
// Makes a sort type that can make a compare function using the SetType | ||
var Sort = BasicQuery.makeSort(keys, hydrateAndValue); | ||
var serializer = new Serializer(serializeMap); | ||
serializer.add(comparisonsConverter.serializer); | ||
return { | ||
hydrate: function(data){ | ||
return { | ||
hydrate: function(data) { | ||
//!steal-remove-start | ||
var AcceptedFields = makeEnum(function(){},["filter","sort","page"]); | ||
var diff = set.difference(new AcceptedFields(Object.keys(data)), AcceptedFields.UNIVERSAL); | ||
if(diff.values && diff.values.length) { | ||
logDev.warn( | ||
"can-query-logic: Ignoring keys: " + diff.values.join(", ") + "." | ||
); | ||
} | ||
//!steal-remove-end | ||
//!steal-remove-start | ||
if (process.env.NODE_ENV !== 'production') { | ||
var AcceptedFields = makeEnum(function() {}, ["filter", "sort", "page"]); | ||
var diff = set.difference(new AcceptedFields(Object.keys(data)), AcceptedFields.UNIVERSAL); | ||
if (diff.values && diff.values.length) { | ||
logDev.warn( | ||
"can-query-logic: Ignoring keys: " + diff.values.join(", ") + "." | ||
); | ||
} | ||
} | ||
//!steal-remove-end | ||
var filter = canReflect.serialize(data.filter); | ||
var filter = canReflect.serialize(data.filter); | ||
// this mutates | ||
var filterAnd = hydrateFilter(filter, keys, helpers.valueHydrator); | ||
// this mutates | ||
var filterAnd = hydrateFilter(filter, keys, helpers.valueHydrator); | ||
// Conver the filter arguments | ||
// Conver the filter arguments | ||
var query = { | ||
filter: filterAnd | ||
}; | ||
if(data.page) { | ||
query.page = new BasicQuery.RecordRange(data.page.start, data.page.end); | ||
} | ||
if(data.sort) { | ||
query.sort = new Sort(data.sort); | ||
} else { | ||
query.sort = new Sort(id); | ||
} | ||
return new BasicQuery(query); | ||
}, | ||
serializer: serializer | ||
}; | ||
var query = { | ||
filter: filterAnd | ||
}; | ||
if (data.page) { | ||
query.page = new BasicQuery.RecordRange(data.page.start, data.page.end); | ||
} | ||
if (data.sort) { | ||
query.sort = new Sort(data.sort); | ||
} else { | ||
query.sort = new Sort(id); | ||
} | ||
return new BasicQuery(query); | ||
}, | ||
serializer: serializer | ||
}; | ||
}; |
@@ -252,5 +252,7 @@ var set = require("../set"); | ||
//!steal-remove-start | ||
Object.defineProperty(ComparisonSetType, "name", { | ||
value: "Or[" + categories.valueOfTypes[0].name + "," + categories.primitives.map(String).join(" ") + "]" | ||
}); | ||
if(process.env.NODE_ENV !== 'production') { | ||
Object.defineProperty(ComparisonSetType, "name", { | ||
value: "Or[" + categories.valueOfTypes[0].name + "," + categories.primitives.map(String).join(" ") + "]" | ||
}); | ||
} | ||
//!steal-remove-end | ||
@@ -257,0 +259,0 @@ } |
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
10133
1
373587
2