Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ldap-filters

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ldap-filters

Library for generating, parsing, and evaluating LDAP filters

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
151
increased by65.93%
Maintainers
1
Weekly downloads
 
Created
Source

node-ldap-filters

Build, generate, parse, and evaluate LDAP filters

A library for working with Lightweight Directory Access Protocol (LDAP) filters based on RFC 4515.

Although this format is typicaly used with the LDAP protocol, this library could be implemented in other applications that need portable string-based filters for the purpose of matching object data.

Usage

Installation

Use npm:

npm install ldap-filters

Build a filter (programatically)

var Filter = require('ldap-filters');

var output = Filter.AND([
  Filter.attribute('givenName').equalTo('jenny'),
  Filter.attribute('sn').equalTo('jensen')
]);

console.log(output.toString());

Note: You must call the .toString() method, to obtain the filter as a string.

Matching methods

Various methods can be used to build simple filters:

  • .present() - tests for presence (attr=*)
  • .equalTo(value) - tests for equality (attr=value)
  • .contains(value) - tests if attribute contains value (attr=*value*)
  • .endsWith(value) - tests if attribute ends with value (attr=*value)
  • .startsWith(value) - tests if attribute starts with value (attr=value*)
  • .approx(value) - tests if value is approximate match (attr~=value)
  • .gte(value) - tests if value is greater than or equal (attr>=value)
  • .lte(value) - tests if value is less than or equal (attr<=value)
Aggregate methods

Simple filters can be aggregated with AND, OR, and NOT:

  • Filter.AND(list) - AND a list (array) of filters (&(f1)(f2)..)
  • Filter.OR(list) - OR a list (array) of filters (|(f1)(f2)..)
  • Filter.NOT(filter) - NOT (negate) a filter (!(filter))

Aggregation and nesting can be used to build complex filters.

Parse a filter

Parses a filter from a string, returning a Filter object.

var Filter = require('ldap-filters');
var input = '(&(givenName=jenny)(sn=jensen))';

Filter.parse(input);

Output/print

Whether you've created a filter programatically or by parsing a filter, you can output with toString() method or by concatenating with a string, like so:

query.toString()
query + ''

If you pass a value of true or a numeric value to toString(), the output will be beautified:

query.toString(true)
query.toString(2)

Will result in the following output:

(&
    (givenName=jenny)
    (sn=jensen)
    (|
        (c=us)
        (st=ontario)
    )
)

A value of true will use Filter.indent property, which defaults to 4.

Evaluate data against a filter

Test if (object) data matches a given filter. The filter can be one created programatically, or parsed from a text string. A boolean true value will be returned for a successful match.

var Filter = require('ldap-filters');
var input = '(&(givenName~=jeni)(sn=jensen))';
var parsed = Filter.parse(input);

var data = { givenName: 'Jenny', sn: 'Jensen' };
console.log(parsed.match(data));

Unit Tests

A complete test suite is included. To run it, you will need to have mocha and chai installed. Mocha should be installed globally (need sudo?).

npm install -g mocha
npm install chai

There are three ways to run the tests:

# Run tests with npm
npm test

# Run tests with "make"
make test

# Run tests manually
mocha test/*.js

Building

The parser is built with jison. To re-build the parser, you must have jison installed globally.

# Install jison globally (need sudo?)
npm install -g jison

# Build with "make"
make parser

# Build manually with jison
jison lib/parser.jison -o lib/parser.js

Credits

The jison parser source was originally written by tantaman found in the DATS-DAP repository.

Keywords

FAQs

Package last updated on 13 Jun 2016

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc