You're Invited:Meet the Socket Team at RSAC and BSidesSF 2026, March 23–26.RSVP
Socket
Book a DemoSign in
Socket

@filtron/js

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@filtron/js

Filtron helper: transform filter expressions into JavaScript predicates for Array.filter()

latest
Source
npmnpm
Version
1.1.2
Version published
Weekly downloads
4
Maintainers
1
Weekly downloads
 
Created
Source

@filtron/js

Convert Filtron AST to JavaScript filter predicates for use with Array.filter().

npm version npm bundle size codecov

Installation

npm install @filtron/js

Usage

import { parse } from "@filtron/core";
import { toFilter } from "@filtron/js";

const result = parse('age > 18 AND status = "active"');

if (result.success) {
  const filter = toFilter(result.ast);

  const users = [
    { name: "Alice", age: 25, status: "active" },
    { name: "Bob", age: 16, status: "active" },
  ];

  users.filter(filter);
  // => [{ name: "Alice", age: 25, status: "active" }]
}

API

toFilter<T>(ast, options?): (item: T) => boolean

Converts a Filtron AST to a predicate function.

Options

OptionTypeDefaultDescription
allowedFieldsstring[]undefinedWhitelist of queryable fields (throws if field not in list)
fieldAccessor(obj: T, field: string) => unknownundefinedCustom function to retrieve field values
caseInsensitivebooleanfalseCase-insensitive string comparisons
fieldMappingRecord<string, string>undefinedMap query field names to object property names

Examples

Restrict allowed fields:

const filter = toFilter(ast, {
  allowedFields: ["name", "email", "age"],
});
// Querying "password" will throw an error

Case-insensitive matching:

const filter = toFilter(ast, {
  caseInsensitive: true,
});
// "status = 'ACTIVE'" matches { status: "active" }

Field mapping:

const filter = toFilter(ast, {
  fieldMapping: {
    email: "emailAddress",
    age: "userAge",
  },
});
// Query "email" maps to object property "emailAddress"

Combined options:

const filter = toFilter(ast, {
  fieldMapping: { user_email: "email" },
  allowedFields: ["user_email"],  // Validates against query field names
  caseInsensitive: true,
});

nestedAccessor(separator?): FieldAccessor

Creates a field accessor for dot-notation nested properties:

import { toFilter, nestedAccessor } from "@filtron/js";

const filter = toFilter(ast, {
  fieldAccessor: nestedAccessor(),
});

// Query: "user.profile.age > 18"
// Matches: { user: { profile: { age: 25 } } }

Custom separator:

const filter = toFilter(ast, {
  fieldAccessor: nestedAccessor("/"),
});
// Query: "user/profile/age > 18"

Security

When accepting user input, use allowedFields to prevent access to sensitive properties:

const filter = toFilter(ast, {
  allowedFields: ["name", "email", "status"],
});
// Queries against "password", "token", etc. will throw

License

MIT

Keywords

array

FAQs

Package last updated on 02 Jan 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts