import assert from 'assert' ; import * as _ from 'lodash' ; import { AnyId , Value , IdArg } from './core' ; declare module './core' { interface AnyId { variable ( name ? ) : AnyId ; } } class VariableValue extends Value { constructor ( owner , private name ? ) { super ( owner ) ; } value ( arg ) { assert ( ! _ . isUndefined ( arg ) , 'Variable requires to be given in id()' ) ; if ( typeof arg === 'number' || arg instanceof Buffer ) { assert ( ! this . name , 'Expect an object to be passed into id()' ) ; return this . returnValue ( arg ) ; } else { if ( this . name ) { assert ( _ . has ( arg , this . name ) , `Missing property ${ this . name } in object passed into id()` ) ; const property = arg [ this . name ] ; if ( typeof property === 'number' || property instanceof Buffer ) { return this . returnValue ( property ) ; } throw new Error ( `Expect variable ${ this . name } to be a number or a Buffer` ) ; } throw new Error ( 'Expect a number or a Buffer to be passed into id() instead of object' ) ; } } } export class Variable { variable ( this , name ? ) { this . addValue ( new VariableValue ( this , name ) ) ; return this ; } }