@uwdata/mosaic-core
Version:
Scalable and extensible linked data views.
126 lines • 5.45 kB
JavaScript
import { isMosaicClient } from './MosaicClient.js';
import { and, contains, isBetween, isIn, isNotDistinct, literal, or, prefix, regexp_matches, suffix } from '@uwdata/mosaic-sql';
/**
* Generate a selection clause for a single selected point value.
* @param field The table column or expression to select.
* @param value The selected value.
* @param options Additional clause properties.
* @param options.source The source component generating this clause.
* @param options.clients The Mosaic clients associated
* with this clause. These clients are not filtered by this clause in
* cross-filtering contexts.
* @returns The generated selection clause.
*/
export function clausePoint(field, value, { source, clients = isMosaicClient(source) ? new Set([source]) : undefined }) {
const predicate = value !== undefined
? isIn(field, [literal(value)])
: null;
return {
meta: { type: 'point' },
source,
clients,
value,
predicate
};
}
/**
* Generate a selection clause for multiple selected point values.
* @param fields The table columns or expressions to select.
* @param value The selected values, as an array of
* arrays. Each subarray contains values for each *fields* entry.
* @param options Additional clause properties.
* @param options.source The source component generating this clause.
* @param options.clients The Mosaic clients associated
* with this clause. These clients are not filtered by this clause in
* cross-filtering contexts.
* @returns The generated selection clause.
*/
export function clausePoints(fields, value, { source, clients = isMosaicClient(source) ? new Set([source]) : undefined }) {
let predicate = null;
if (value) {
const clauses = value.length && fields.length === 1
? [isIn(fields[0], value.map(v => literal(v[0])))]
: value.map(v => and(v.map((_, i) => isNotDistinct(fields[i], literal(_)))));
predicate = value.length === 0 ? literal(false)
: clauses.length > 1 ? or(clauses)
: clauses[0];
}
return {
meta: { type: 'point' },
source,
clients,
value,
predicate
};
}
/**
* Generate a selection clause for a selected 1D interval.
* @param field The table column or expression to select.
* @param value The selected interval as a [lo, hi] array.
* @param options Additional clause properties.
* @param options.source The source component generating this clause.
* @param options.clients The Mosaic clients associated
* with this clause. These clients are not filtered by this clause in
* cross-filtering contexts.
* @param options.scale The scale mapping descriptor.
* @param options.bin A binning method hint.
* @param options.pixelSize The interactive pixel size.
* @returns The generated selection clause.
*/
export function clauseInterval(field, value, { source, clients = isMosaicClient(source) ? new Set([source]) : undefined, bin, scale, pixelSize = 1 }) {
const predicate = value != null ? isBetween(field, value) : null;
const meta = {
type: 'interval',
scales: scale && [scale],
bin,
pixelSize
};
return { meta, source, clients, value, predicate };
}
/**
* Generate a selection clause for multiple selected intervals.
* @param fields The table columns or expressions to select.
* @param value The selected intervals, as an array of extents.
* @param options Additional clause properties.
* @param options.source The source component generating this clause.
* @param options.clients The Mosaic clients associated
* with this clause. These clients are not filtered by this clause in
* cross-filtering contexts.
* @param options.scales The scale mapping descriptors,
* in an order matching the given *fields* and *value* extents.
* @param options.bin A binning method hint.
* @param options.pixelSize The interactive pixel size.
* @returns The generated selection clause.
*/
export function clauseIntervals(fields, value, { source, clients = isMosaicClient(source) ? new Set([source]) : undefined, bin, scales = [], pixelSize = 1 }) {
const predicate = value != null
? and(fields.map((f, i) => isBetween(f, value[i])))
: null;
const meta = {
type: 'interval',
scales,
bin,
pixelSize
};
return { meta, source, clients, value, predicate };
}
const MATCH_METHODS = { contains, prefix, suffix, regexp: regexp_matches };
/**
* Generate a selection clause for text search matching.
* @param field The table column or expression to select.
* @param value The selected text search query string.
* @param options Additional clause properties.
* @param options.source The source component generating this clause.
* @param options.clients The Mosaic clients associated
* with this clause. These clients are not filtered by this clause in
* cross-filtering contexts.
* @param options.method The text matching method to use. Defaults to `'contains'`.
* @returns The generated selection clause.
*/
export function clauseMatch(field, value, { source, clients = undefined, method = 'contains' }) {
const fn = MATCH_METHODS[method];
const predicate = value ? fn(field, literal(value)) : null;
const meta = { type: 'match', method };
return { meta, source, clients, value, predicate };
}
//# sourceMappingURL=SelectionClause.js.map