@collabland/common
Advanced tools
Comparing version 0.36.0 to 0.37.0
import { Expression, Focus } from 'jsonata'; | ||
import { IOptions } from 'minimatch'; | ||
import { AnyType } from '../types'; | ||
import { BNLike } from './bignumber'; | ||
@@ -18,14 +19,5 @@ export { IOptions as GlobOptions } from 'minimatch'; | ||
trait_type: string; | ||
value: string; | ||
value: AnyType; | ||
}; | ||
/** | ||
* Normalize trait to be lowercase trimmed type/value pair | ||
* @param trait - Trait like object | ||
* @returns | ||
*/ | ||
export declare function normalizeTrait(trait: { | ||
trait_type: string; | ||
value: unknown; | ||
}): Trait; | ||
/** | ||
* Filter to match a list of trait type/value pairs against an individual token | ||
@@ -32,0 +24,0 @@ * using `and` or `or` condition |
@@ -7,3 +7,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.wildcardToRegExp = exports.globToRegExp = exports.isGlobPattern = exports.isInRanges = exports.parseRanges = exports.jsonQuery = exports.globalJsonQueryFunctions = exports.normalizeTrait = void 0; | ||
exports.wildcardToRegExp = exports.globToRegExp = exports.isGlobPattern = exports.isInRanges = exports.parseRanges = exports.jsonQuery = exports.globalJsonQueryFunctions = void 0; | ||
const tslib_1 = require("tslib"); | ||
@@ -211,13 +211,28 @@ const bignumber_1 = require("@ethersproject/bignumber"); | ||
/** | ||
* Normalize trait to be lowercase trimmed type/value pair | ||
* @param trait - Trait like object | ||
* Evaluate the condition against a trait | ||
* @param condition - Trait condition | ||
* @param trait - Trait | ||
* @returns | ||
*/ | ||
function normalizeTrait(trait) { | ||
return { | ||
trait_type: trait.trait_type == null ? '' : trait.trait_type.toLowerCase().trim(), | ||
value: trait.value == null ? '' : String(trait.value).toLowerCase().trim(), | ||
}; | ||
function evaluateTrait(condition, trait) { | ||
const conditionType = condition.trait_type.trim().toLowerCase(); | ||
const traitType = trait.trait_type.trim().toLowerCase(); | ||
if (conditionType !== traitType) | ||
return false; | ||
if (condition.value.startsWith('$')) { | ||
debug('Evaluating %O as a query against %O', condition, trait); | ||
// Treat it as a JSONata query expression, such as $ >= 10 | ||
const result = jsonQuery(condition.value)(trait.value); | ||
return !!result; | ||
} | ||
const conditionValue = condition.value; | ||
const traitValue = trait.value; | ||
// eslint-disable-next-line eqeqeq | ||
if (conditionValue == traitValue) | ||
return true; | ||
if (typeof conditionValue === 'string' && typeof traitValue === 'string') { | ||
return (conditionValue.trim().toLowerCase() === traitValue.trim().toLowerCase()); | ||
} | ||
return false; | ||
} | ||
exports.normalizeTrait = normalizeTrait; | ||
function matchesTraitsPerToken(value, filter) { | ||
@@ -233,9 +248,7 @@ var _a, _b; | ||
} | ||
const fn = (t) => { | ||
t = normalizeTrait(t); | ||
const fn = (condition) => { | ||
const result = attrs.some(a => { | ||
a = normalizeTrait(a); | ||
return t.trait_type === a.trait_type && t.value === a.value; | ||
return evaluateTrait(condition, a); | ||
}); | ||
debug('Matching trait %O against %O: %s', t, attrs, result); | ||
debug('Matching trait %O against %O: %s', condition, attrs, result); | ||
return result; | ||
@@ -242,0 +255,0 @@ }; |
@@ -62,3 +62,3 @@ import pMapFn from 'p-map'; | ||
*/ | ||
export declare function fetchIterator<VALUE, STATE, SUMMARY = undefined>(options: AsyncFetchOptions<VALUE, STATE, SUMMARY>): AsyncGenerator<Awaited<VALUE>, Awaited<SUMMARY> | undefined, unknown>; | ||
export declare function fetchIterator<VALUE, STATE, SUMMARY = undefined>(options: AsyncFetchOptions<VALUE, STATE, SUMMARY>): AsyncGenerator<Awaited<NonNullable<VALUE>>, Awaited<SUMMARY> | undefined, unknown>; | ||
export interface AsyncFetchByPageOptions<VALUE, STATE> { | ||
@@ -65,0 +65,0 @@ next: (state?: STATE) => Promise<{ |
{ | ||
"name": "@collabland/common", | ||
"version": "0.36.0", | ||
"version": "0.37.0", | ||
"description": "CollabLand common utilities", | ||
@@ -24,9 +24,2 @@ "main": "dist/index.js", | ||
}, | ||
"license": "MIT", | ||
"files": [ | ||
"README.md", | ||
"dist", | ||
"src", | ||
"!*/__tests__" | ||
], | ||
"dependencies": { | ||
@@ -56,12 +49,19 @@ "@ethersproject/bignumber": "^5.7.0", | ||
"devDependencies": { | ||
"@loopback/build": "^9.0.3", | ||
"@loopback/eslint-config": "^13.0.3", | ||
"@loopback/testlab": "^5.0.3", | ||
"@loopback/build": "^9.0.4", | ||
"@loopback/eslint-config": "^13.0.4", | ||
"@loopback/testlab": "^5.0.4", | ||
"@types/minimatch": "3.0.5", | ||
"@types/node": "^16.11.57", | ||
"typescript": "~4.7.4" | ||
"@types/node": "^16.11.65", | ||
"typescript": "~4.8.4" | ||
}, | ||
"license": "MIT", | ||
"files": [ | ||
"README.md", | ||
"dist", | ||
"src", | ||
"!*/__tests__" | ||
], | ||
"copyright.owner": "Abridged, Inc.", | ||
"author": "Abridged, Inc.", | ||
"gitHead": "a8661b5369a196c22e1b3d1f0ac3ba2235cc5453" | ||
"gitHead": "af3022302cc7452b01ef6bd81fb3052863da50c8" | ||
} |
@@ -251,21 +251,5 @@ // Copyright Abridged, Inc. 2021. All Rights Reserved. | ||
*/ | ||
export type Trait = {trait_type: string; value: string}; | ||
export type Trait = {trait_type: string; value: AnyType}; | ||
/** | ||
* Normalize trait to be lowercase trimmed type/value pair | ||
* @param trait - Trait like object | ||
* @returns | ||
*/ | ||
export function normalizeTrait(trait: { | ||
trait_type: string; | ||
value: unknown; | ||
}): Trait { | ||
return { | ||
trait_type: | ||
trait.trait_type == null ? '' : trait.trait_type.toLowerCase().trim(), | ||
value: trait.value == null ? '' : String(trait.value).toLowerCase().trim(), | ||
}; | ||
} | ||
/** | ||
* Filter to match a list of trait type/value pairs against an individual token | ||
@@ -320,2 +304,30 @@ * using `and` or `or` condition | ||
/** | ||
* Evaluate the condition against a trait | ||
* @param condition - Trait condition | ||
* @param trait - Trait | ||
* @returns | ||
*/ | ||
function evaluateTrait(condition: Trait, trait: Trait) { | ||
const conditionType = condition.trait_type.trim().toLowerCase(); | ||
const traitType = trait.trait_type.trim().toLowerCase(); | ||
if (conditionType !== traitType) return false; | ||
if (condition.value.startsWith('$')) { | ||
debug('Evaluating %O as a query against %O', condition, trait); | ||
// Treat it as a JSONata query expression, such as $ >= 10 | ||
const result = jsonQuery(condition.value)(trait.value); | ||
return !!result; | ||
} | ||
const conditionValue = condition.value; | ||
const traitValue = trait.value; | ||
// eslint-disable-next-line eqeqeq | ||
if (conditionValue == traitValue) return true; | ||
if (typeof conditionValue === 'string' && typeof traitValue === 'string') { | ||
return ( | ||
conditionValue.trim().toLowerCase() === traitValue.trim().toLowerCase() | ||
); | ||
} | ||
return false; | ||
} | ||
function matchesTraitsPerToken(value: AnyType, filter: TraitFilterPerToken) { | ||
@@ -330,9 +342,7 @@ let attrs: Trait[] = value?.metadata?.attributes ?? []; | ||
} | ||
const fn = (t: Trait) => { | ||
t = normalizeTrait(t); | ||
const fn = (condition: Trait) => { | ||
const result = attrs.some(a => { | ||
a = normalizeTrait(a); | ||
return t.trait_type === a.trait_type && t.value === a.value; | ||
return evaluateTrait(condition, a); | ||
}); | ||
debug('Matching trait %O against %O: %s', t, attrs, result); | ||
debug('Matching trait %O against %O: %s', condition, attrs, result); | ||
return result; | ||
@@ -339,0 +349,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
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
205795
5007