UNPKG

@uwdata/mosaic-core

Version:

Scalable and extensible linked data views.

168 lines 7.84 kB
import { isMosaicClient } from './MosaicClient.js'; import { and, contains, isBetween, isInDistinct, isNotDistinct, listHasAll, listHasAny, literal, lower, 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 ? isInDistinct(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?.length) { const clauses = value.length && fields.length === 1 ? [isInDistinct(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 identity = (x) => x; const MATCH_METHODS = { contains, prefix, suffix, regexp: regexp_matches }; /** * Generate a selection clause for text search matching over a single column. * @param field The table column or expression to match. * @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 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, default `'contains'`. * @param options.caseSensitive Flag for case sensitive matching, default `false`. * @returns The generated selection clause. */ export function clauseMatch(field, value, { source, clients = undefined, method = 'contains', caseSensitive = false }) { const fn = MATCH_METHODS[method]; const transform = caseSensitive ? identity : lower; const predicate = value ? fn(transform(field), transform(literal(value))) : null; const meta = { type: 'match', method }; return { meta, source, clients, value, predicate }; } /** * Generate a selection clause for text search matching over multiple columns. * A match will succeed if any field successfully matches. * @param fields The table columns or expressions to match. * @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 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, default `'contains'`. * @param options.caseSensitive Flag for case sensitive matching, default `false`. * @returns The generated selection clause. */ export function clauseMatchAny(fields, value, { source, clients = undefined, method = 'contains', caseSensitive = false }) { value = value || null; const fn = MATCH_METHODS[method]; const transform = caseSensitive ? identity : lower; const query = transform(literal(value)); const predicate = value ? or(fields.flatMap(field => value ? fn(transform(field), query) : [])) : null; const meta = { type: 'match', method }; return { meta, source, clients, value, predicate }; } /** * Generate a selection clause for a single selected point value in a list. * @param field The table column or expression to select, which must be a list. * @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 clauseList(field, value, { source, clients = isMosaicClient(source) ? new Set([source]) : undefined, listMatch = 'any' }) { const listFn = listMatch === 'all' ? listHasAll : listHasAny; const predicate = value !== undefined ? listFn(field, literal(value)) : null; return { source, clients, value, predicate }; } //# sourceMappingURL=SelectionClause.js.map