0.1.2 • Published 6 years ago
logical-object-match v0.1.2
logical-object-match
Logical match of a JS object based on a definition. (Inspired from Prisma's filtering system)
Installation
npm install logical-object-matchUsage
You can match a single object based on a definition
import matcher from 'logical-object-match'
const movie = {
title: 'Lost in Translation',
rating: 95,
genre: 'Comedy',
cast: ['Bill Murray', 'Scarlett Johansson']
}
matcher(movie, {
title: 'Lost in Translation',
genre: 'Comedy'
}) // true
matcher(movie, {
AND: [
{ rating_gte: 90 },
{ title_starts_with: 'Lost' },
{
OR: [
{ genre_in: ['Comedy', 'Thriller'] },
{ cast_not_contains: 'Jude Law' },
{ NOT: [{ rating_lt: 50 }] }
]
}
]
}) // trueOr you can filter an array of objects based on a definition
import matcher from 'logical-object-match'
const movies = [
{
title: 'Lost in Translation',
rating: 95
},
{
title: 'John Wick',
rating: 86
},
{
title: 'The Last Airbender',
rating: 5
}
]
const goodMovies = movies.filter(movie => matcher(movie, { rating_gte: 85 })) // Will return an array with the Lost in Translation and John Wick objectsDocumentation
Operations
You can use the filter combinators OR, AND and NOT to create an arbitrary logical combination of matching conditions:
- For an
AND-filter to evaluate to true, all of the nested conditions have to betrue.
let def = {
AND: [
{ title: 'Lost in Translation' },
{ rating_gte: 90 },
{ genre_in: ['Comedy', 'Thriller'] },
{ cast_contains: 'Bill Murray' }
]
}- For an
OR-filter to evaluate to true, at least one of the nested conditions has to betrue.
let def = {
OR: [
{ title: 'John Wick' },
{ rating_gte: 90 },
{ genre_in: ['Drame', 'Thriller'] },
{ cast_contains: 'Scarlett Johansson' }
]
}- For a
NOT-filter to evaluate to true, all of the nested conditions have to befalse.
let def = {
NOT: [
{ title: 'The Last Airbender' },
{ rating_lt: 50 },
{ genre_not_in: ['Comedy', 'Thriller'] },
{ cast_contains: 'Jude Law' }
]
}Suffixes
You can use suffixes for more matching conditions:
_notrequires the attribute not to be the given value
let def = {
title_not: 'Star Wars'
}_inand_not_inrequires the attribute to be one of the given value
let def = {
genre_in: ['Comedy', 'Thriller']
}
let def = {
genre_not_in: ['Drame', 'Thriller']
}_ltand_lterequires the attribute to be less than (or equal) the given number
let def = {
rating_lt: 98
}
let def = {
rating_lte: 98
}_gtand_gterequires the attribute to be greater than (or equal) the given number
let def = {
rating_gt: 50
}
let def = {
rating_gte: 50
}_containsand_not_containsrequires the attribute to contains the given value
let def = {
cast_contains: 'Scarlett Johansson'
}
let def = {
cast_not_contains: 'Jude Law'
}_starts_withand_not_starts_withrequires the attribute to starts with the given string
let def = {
title_starts_with: 'Lost'
}
let def = {
title_not_starts_with: 'Star'
}_ends_withand_not_ends_withrequires the attribute to ends with the given string
let def = {
title_ends_with: 'Translation'
}
let def = {
title_not_ends_with: 'Wars'
}_everyrequires all items of the array to match the given definition
let def = {
movies_every: {
rating_gte: 80
}
}_somerequires at least one of the items of the array to match the given definition
let def = {
movies_some: {
rating_gte: 90
}
}_everyrequires that none of the items of the array match the given definition
let def = {
movies_none: {
rating_gte: 60
}
}