1.1.1 • Published 6 years ago

ts-pair v1.1.1

Weekly downloads
21
License
MIT
Repository
github
Last release
6 years ago

ts-pair

Pair classes for TypeScript and JavaScript

Install

npm install ts-pair --save

Example

let pair = new Pair( 1, "Hello" );
console.log( pair.first, ', ', pair.second ); // 1, Hello
console.log( 'Pair is ' + pair ); // Pair is [ 1, Hello ]

pair.first = 2;
pair.second = "hi";

console.log( 'JSON', pair.toJSON() ); // Pair is { "first": 2, "second": "hi" }

let [ first, second ] = pair.toArray();
console.log( first, second ); // 2, hi

// chainable
console.log( pair.setFirst( 10 ).setSecond( "bye" ).toString() ); // [ 10, bye ]

let otherPair = new Pair( 20, "bye" );
console.log( otherPair.equals( pair ) ? 'equal': 'different' ); // different

let aImmutable = new ImmutablePair( 20, "bye" );
console.log( aImmutable.equals( otherPair ) ? 'equal': 'different' ); // equal

aImmutable.first = "baz"; // Error

Available Types

  • IPair interface, with getFirst(), getSecond(), equals(), toArray(), and toJSON()
  • AbstractPair that implements IPair and declares toString()
  • Pair that extends AbstractPair
  • ImmutablePair that extends AbstractPair

License

MIT © Thiago Delgado Pinto