npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@moregeo-it/cql2

v0.1.0

Published

Parse, validate and write OGC CQL2 filters in the text and JSON encodings

Readme

cql2-js

Parse, validate and write OGC CQL2 filters in JavaScript.

  • Reads and writes both encodings, CQL2 text and CQL2 JSON, through one set of classes.
  • Covers the whole standard: comparisons, LIKE, BETWEEN, IN, case- and accent-insensitive comparison, spatial, temporal and array operators, functions and arithmetic.
  • Checks filters against the conformance classes a server supports, against queryables with their types, and against the functions a server offers.
  • Errors say where the problem is: a position in the text, or a path in the JSON.
  • No dependencies. Runs in Node.js and in the browser.

It does not evaluate filters and does not generate SQL.

Install

npm install @moregeo-it/cql2

Parse and write

import { parse, parseText, parseJson } from '@moregeo-it/cql2';

const filter = parseText("eo:cloud_cover < 10 AND platform IN ('sentinel-2a', 'sentinel-2b')");

filter.toJSON();
// { op: 'and', args: [
//   { op: '<', args: [{ property: 'eo:cloud_cover' }, 10] },
//   { op: 'in', args: [{ property: 'platform' }, ['sentinel-2a', 'sentinel-2b']] } ] }

parseJson({ op: '=', args: [{ property: 'id' }, "Tom's"] }).toText();
// "id = 'Tom''s'"

parse(input); // detects the encoding

A filter is a tree of nodes. Operators have an operator and args, values have a value, properties have a name. Every node has toText() and toJSON(). Nodes parsed from text carry their position in span, nodes parsed from JSON carry their path.

Build filters

import { CqlAnd, CqlBetween, CqlEqual, CqlProperty, CqlValue } from '@moregeo-it/cql2';

const filter = new CqlAnd([
  new CqlEqual(new CqlProperty('platform'), CqlValue.create('landsat-9')),
  new CqlBetween(new CqlProperty('eo:cloud_cover'), CqlValue.create(0), CqlValue.create(10))
]);

filter.toText(); // "platform = 'landsat-9' AND eo:cloud_cover BETWEEN 0 AND 10"

CqlValue.create() turns strings, numbers, booleans, Date objects, arrays and GeoJSON geometries into nodes. Any object with toText() and toJSON() can be used as an argument, so an application can keep its own classes for properties.

Validate

import { parseText, validate } from '@moregeo-it/cql2';

const problems = validate(parseText("eo:cloud_cover = 'low' AND S_WITHIN(geometry, BBOX(0, 0, 1, 1))"), {
  queryables: {
    'eo:cloud_cover': { type: 'number' },
    geometry: { format: 'geometry-any' }
  },
  conformance: ['basic-cql2', 'basic-spatial-functions']
});
// [ { code: 'type-mismatch', message: 'The right side of = is of type string, but is compared with type number',
//     span: { start: 17, end: 22 }, ... },
//   { code: 'needs-conformance', conformance: 'http://www.opengis.net/spec/cql2/1.0/conf/spatial-functions', ... } ]

validate() collects all problems and returns an empty list for a valid filter. All options are optional.

| Option | Meaning | |---|---| | queryables | Property names with their JSON Schema, as in the properties of a Queryables document, or with a type name | | additionalProperties | Set to true to allow properties that are not listed in queryables | | conformance | The CQL2 conformance classes the server supports, as URIs or short names | | functions | The functions the server offers, each with optional args and returns types |

| Problem code | Meaning | |---|---| | type-mismatch | An operand has the wrong type | | unknown-property | A property is not a queryable | | unknown-function | A function is not offered by the server | | needs-conformance | The filter uses a conformance class the server does not support | | invalid-pattern | The pattern of LIKE is not a string | | not-boolean | The filter as a whole is not a boolean expression |

requiredConformance(filter) lists the conformance classes a filter needs.

Errors

Both parsers throw a CqlError.

try {
  parseText('a = 1 AND (b = 2');
} catch (error) {
  error.message;  // 'Expected ")" but found the end of the filter (line 1, column 17)'
  error.position; // 16
}

For JSON input the error has a path, for example ['args', 1, 'date'].

Filters whose tree is deeper than 100 levels are refused. The limit can be changed with the maxDepth option of the parsers.

Decisions where the standard leaves room

  • Input is not rewritten. Timestamps, numbers and names are written back as they were read. Only keywords and operator names get their standard spelling.
  • Timestamps must be in UTC and end in Z, as the standard requires.
  • a NOT LIKE b, a NOT BETWEEN b AND c, a NOT IN (b) and a IS NOT NULL are read as not around the operator, and written back in the short form.
  • Chains of AND or OR in text become one operator with several arguments.
  • A minus sign in front of a number is part of the number. In front of anything else it is read as a multiplication with -1.
  • Parentheses around a whole array element or a whole function argument are an array with one element. Parentheses around a part of it, and everywhere else, are a group.
  • Geometries with measures (M) are refused. Positions with three numbers are written with Z.
  • Property names that are not identifiers, such as my-prop, are written in double quotes, although the grammar only allows identifiers inside the quotes, see ogcapi-features#916.
  • Property names that are keywords of the standard, such as date, are written in double quotes. They are read with and without quotes where the meaning is clear.
  • An IN list without values is refused in text, as in the grammar. In JSON it is accepted, as in the schema.
  • Polygon rings must be closed, and lines need at least two positions, in both encodings.
  • Integers written with more digits than JavaScript keeps, and numbers that are not finite, are refused instead of being rounded, when reading and when writing, including coordinates and bounding boxes. Numbers with up to 15 significant digits, such as 1e30, are approximate by notation and accepted.
  • In strings, the backslash escapes of the standard are read and written. A backslash itself is written as \\, and a backslash in front of any other character is kept. The standard does not define this, see ogcapi-features#1133.
  • The depth limit applies to the resulting tree, so a chain of about 100 arithmetic operators is refused like deeper nesting. AND and OR chains are flat and have no limit.
  • Functions cannot be named like a keyword of the standard, such as date or point, because their call could not be told apart in text.

Tests

The tests run all example filters of the standard and the expressions of its abstract test suite, and check the written JSON against the JSON schema of the standard. See NOTICE for the licence of this material.

npm test
npm run lint
npm run typecheck

Releases

A release is published to npm by the workflow publish.yml when a GitHub release is created. The tag must be v followed by the version in package.json. The workflow uses npm trusted publishing, so no token is stored in the repository.

Licence

Apache-2.0