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

In-memory JavaScript array filtering using Filtron AST

Source
npmnpm
Version
1.0.0
Version published
Weekly downloads
4
Maintainers
1
Weekly downloads
 
Created
Source

@filtron/js

In-memory JavaScript array filtering using Filtron AST.

Installation

bun add @filtron/js
# or
npm install @filtron/js

Quick Start

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" },
  ];

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

API

toFilter(ast, options?)

Converts a Filtron AST to a predicate function for use with Array.filter().

Options:

OptionTypeDescription
allowedFieldsstring[]Restrict queryable fields (throws if field not in list)
fieldAccessor(obj, field) => unknownCustom field value accessor
caseInsensitivebooleanCase-insensitive string comparisons (default: false)
fieldMappingRecord<string, string>Map query field names to object property names
const filter = toFilter(ast, {
  allowedFields: ["name", "email", "age"],
  caseInsensitive: true,
});

nestedAccessor(separator?)

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 } } }

Advanced Usage

Field Mapping

Map query field names to different object property names. This is useful when you want to expose a different API in your queries than your internal data structure:

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

const result = parse('email = "user@example.com" AND age > 18');

if (result.success) {
  const filter = toFilter(result.ast, {
    fieldMapping: {
      email: "emailAddress",
      age: "userAge",
    },
  });

  const users = [
    { emailAddress: "user@example.com", userAge: 25 },
    { emailAddress: "other@example.com", userAge: 16 },
  ];

  const filtered = users.filter(filter);
  // [{ emailAddress: "user@example.com", userAge: 25 }]
}

Field mapping works with all expression types and can be combined with other options:

const filter = toFilter(ast, {
  fieldMapping: {
    user_id: "id",
    user_email: "email",
  },
  allowedFields: ["user_id", "user_email"], // Validation uses query field names (before mapping)
  caseInsensitive: true,
});

Security

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

const filter = toFilter(ast, {
  allowedFields: ["name", "email", "status"], // "password" queries will throw
});

Keywords

filtron

FAQs

Package last updated on 06 Dec 2025

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