What is ltgt?
The 'ltgt' npm package provides utility functions for comparing values with less than, greater than, less than or equal to, and greater than or equal to operations. It is particularly useful for range queries and sorting operations.
What are ltgt's main functionalities?
Less Than Comparison
This feature allows you to compare if one value is less than another. In this example, it checks if 3 is less than 5.
const ltgt = require('ltgt');
const result = ltgt.compare(3, 5, '<');
console.log(result); // true
Greater Than Comparison
This feature allows you to compare if one value is greater than another. In this example, it checks if 7 is greater than 5.
const ltgt = require('ltgt');
const result = ltgt.compare(7, 5, '>');
console.log(result); // true
Range Query
This feature allows you to check if a value falls within a specified range. In this example, it checks if 5 is within the range of 1 to 10.
const ltgt = require('ltgt');
const range = { start: 1, end: 10 };
const result = ltgt.contains(range, 5);
console.log(result); // true
Less Than or Equal To Comparison
This feature allows you to compare if one value is less than or equal to another. In this example, it checks if 5 is less than or equal to 5.
const ltgt = require('ltgt');
const result = ltgt.compare(5, 5, '<=');
console.log(result); // true
Greater Than or Equal To Comparison
This feature allows you to compare if one value is greater than or equal to another. In this example, it checks if 5 is greater than or equal to 5.
const ltgt = require('ltgt');
const result = ltgt.compare(5, 5, '>=');
console.log(result); // true
Other packages similar to ltgt
lodash
Lodash is a modern JavaScript utility library delivering modularity, performance, and extras. It provides a wide range of utility functions for common programming tasks, including comparison operations. Compared to 'ltgt', Lodash offers a broader set of functionalities beyond just comparison operations.
underscore
Underscore is a JavaScript library that provides a whole mess of useful functional programming helpers without extending any built-in objects. It includes functions for comparison and range queries, similar to 'ltgt', but also offers a wide array of other utility functions.
ramda
Ramda is a practical functional library for JavaScript programmers. It makes it easy to create functional pipelines and includes a variety of utility functions, including comparison operations. Ramda focuses on functional programming paradigms, which can be more complex but also more powerful compared to 'ltgt'.
ltgt
implement correct ranges for level-*
example
var ltgt = require('ltgt')
ltgt.start(range)
ltgt.end(range)
ltgt.lowerBound(range)
ltgt.upperBound(range)
ltgt.lt(range)
ltgt.gt(range)
ltgt.lte(range)
ltgt.gte(range)
ltgt.reverse(range)
var filter = ltgt.filter(range)
filter(key) == true
ltgt.contains(range, key)
ways to specify ranges
there have been a variety of ways to specify ranges in level-*.
this module supports them all.
gt/gte, lt/lte
specify a range between a lower bound (gt, gte) and an upper bound (lt, lte)
if gte
and gt
is undefined, read from the start of the database,
if lte
and lt
is undefined, read until the end of the database,
min, max
legacy level-sublevel style,
synonym for gte
, lte
.
start, end, reverse
legacy levelup style.
The range is from start
-> end
, start
does not specify the lowest
record, instead it specifies the first record to be read. However,
reverse
must also be passed correctly. This is way to specify a range is
confusing if you need to read in reverse,
so it's strongly recommended to use gt/gte,lt/lte
.
If reverse
is true
,
start
must be undefined
or less than end
,
unless end
is undefined
.
if reverse
is false
end
must be undefined
or greater than start
,
unless start
is undefined
.
if start is undefined, read from the first record in the database
if end is undefined read until the last record in the database.
api
ltgt.contains(range, key, compare)
using the provided compare method, return true
if key
is within range
. compare defaults to ltgt.compare
ltgt.filter(range, compare)
return a function that returns true if it's argument is within range.
can be passed to Array.filter
[1,2,3,4,5].filter(ltgt.filter({gt: 2, lte: 4})
ltgt.lowerBound(range)
return the lower bound of range
.
Incase the lower bound is specified with gt
,
check ltgt.lowerBoundExclusive
ltgt.upperBound(range)
return the upperBound of range
.
Incase the upper bound is specified with gt
,
check ltgt.upperBoundExclusive
ltgt.lowerBoundExclusive(range)
return true if upper bound is exclusive.
ltgt.upperBoundExclusive(range)
return true if lower bound is exclusive.
ltgt.toLtgt(range, _range, map, lowerBound, upperBound)
convert a range to a new ltgt range. _range
is the object to return - if you want to mutate range
call ltgt.toLtgt(range, range, map)
map
gets called on each key in the range, and wether it's an upper or lower bound -
so can be used as an encode function.
map(value, isUpperBound)
if isUpperBound
is false, this is the lower bound.
License
MIT