
Research
/Security News
10 npm Typosquatted Packages Deploy Multi-Stage Credential Harvester
Socket researchers found 10 typosquatted npm packages that auto-run on install, show fake CAPTCHAs, fingerprint by IP, and deploy a credential stealer.
@activix/spec-pattern
Advanced tools
Implementation of the Specification Pattern for JavaScript and TypeScript.
Useful for building complex filters or rules in a easy way.
No external dependencies. Fully tested. Adopts semantic versioning. Forks are welcome!
$ npm install spec-pattern --save
import { Between } from 'spec-pattern';
let rules = new Between( 1, 3 );
console.log( rules.isSatisfiedBy( 2 ) ); // true
import { Between } from 'spec-pattern';
let rules = new Between( 1, 3 )
   .or( new Between( 6, 9 ) );
console.log( rules.isSatisfiedBy( 2 ) ); // true
console.log( rules.isSatisfiedBy( 7 ) ); // true
console.log( rules.isSatisfiedBy( 5 ) ); // false
import { Between, In, GreaterThan } from 'spec-pattern';
let rules = new Between( 1, 3 )
   .or( new Between( 6, 9 ) )
   .or( new In( [ 11, 25, 31 ] )
   .or( new GreaterThan( 50 ) );
console.log( rules.isSatisfiedBy( 2 ) ); // true
console.log( rules.isSatisfiedBy( 7 ) ); // true
console.log( rules.isSatisfiedBy( 5 ) ); // false
console.log( rules.isSatisfiedBy( 11 ) ); // true
console.log( rules.isSatisfiedBy( 50 ) ); // false
console.log( rules.isSatisfiedBy( 51 ) ); // true
import { StartsWith, Contains } from 'spec-pattern';
let rules = new StartsWith( 'Hello' )
    .andNot( new Contains( 'world' ) );
console.log( rules.isSatisfiedBy( 'Hello Bob' ) ); // true
console.log( rules.isSatisfiedBy( 'Hello world' ) ); // false
import { LengthBetween, EqualTo } from 'spec-pattern';
let rules = new LengthBetween( 2, 5 )
    .andNot( new EqualTo( 'Hello' ) );
console.log( rules.isSatisfiedBy( '' ) ); // false
console.log( rules.isSatisfiedBy( 'Hi' ) ); // true
console.log( rules.isSatisfiedBy( 'Hello' ) ); // false
console.log( rules.isSatisfiedBy( 'Howdy' ) ); // true
console.log( rules.isSatisfiedBy( 'Hello world' ) ); // false
EqualTo( value: any )GreaterThan( value: any )GreaterThanOrEqualTo( value: any )LessThan( value: any )LessThanOrEqualTo( value: any )Between( min: any, max: any )In( values: array )StartsWith( value: string, ignoreCase: boolean = false )EndsWith( value: string, ignoreCase: boolean = false )Contains( value: string, ignoreCase: boolean = false )LengthBetween( min: any, max: any )Matches( regex: RegExp )All these classes extend the abstract class Composite, which in turn implements the interface Spec:
interface Spec< T > {
    isSatisfiedBy( candidate: T ): boolean;
    and( other: Spec< T > ): Spec< T >;
    andNot( other: Spec< T > ): Spec< T >;
    or( other: Spec< T > ): Spec< T >;
    orNot( other: Spec< T > ): Spec< T >;
    not(): Spec< T >;
}
Creating your own class is very easy. Just extends abstract class Composite_, like in the following example. Of course, you can also extend one of the aforementioned classes or implement the interface Spec_ (but why reinventing the wheel, right?).
Let's create a class DifferentFrom_ ...
...in TypeScript:
import { Composite } from 'spec-pattern';
export class DifferentFrom< T > extends Composite< T > {
    constructor( private _value: T ) {
        super();
    }
    isSatisfiedBy( candidate: T ): boolean {
        return this._value != candidate;
    }
    toString(): string {
        return 'different from ' + this._value;
    }
}
...or in JavaScript 6+:
import { Composite } from 'spec-pattern';
class DifferentFrom extends Composite {
    constructor( value ) {
        this._value = value;
    }
    isSatisfiedBy( candidate ) {
        return this._value != candidate;
    }
    toString() {
        return 'different from ' + this._value;
    }
}
...or in JavaScript 5+:
var Composite  = require( 'spec-pattern' ).Composite;
function DifferentFrom( value ) {
    Composite.call( this ); // super()
    this._value = value;
    this.isSatisfiedBy = function ( candidate ) {
        return this._value != candidate;
    };
    this.toString = function() {
        return 'different from ' + this._value;
    };
}
DifferentFrom.prototype = Object.create( Composite.prototype );
DifferentFrom.prototype.constructor = DifferentFrom;
That's it! Just three methods: constructor, isSatisfiedBy, and toString().
FAQs
Specification Pattern in TypeScript
We found that @activix/spec-pattern demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?

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.

Research
/Security News
Socket researchers found 10 typosquatted npm packages that auto-run on install, show fake CAPTCHAs, fingerprint by IP, and deploy a credential stealer.

Product
Socket Firewall Enterprise is now available with flexible deployment, configurable policies, and expanded language support.

Security News
Open source dashboard CNAPulse tracks CVE Numbering Authorities’ publishing activity, highlighting trends and transparency across the CVE ecosystem.