lib/type/typedef.js
function encode(key, value, stringify) {
  var space = ' ', val = [], i, type = this.type;

  function coerce(type, val) {
    var out
      , enumerable = Array.isArray(type);
    if(enumerable) {
      if(!~type.indexOf(val)) {
        throw new TypeDefError(
          util.format('enum error on key \'%s\', got \'%s\' expected one of %s',
            key, val, type.join(', ')));
      }

      return '' + val;
    }
    switch(type) {
      case ByteSize:
        if(typeof val !== 'number') {
          throw new TypeDefError(
            util.format(
              'byte size number expected for \'%s\', got %s', key, val));
        }
        out = ByteSize.humanize(val);
        break;
      case Boolean:
        if(typeof val !== 'boolean') {
          throw new TypeDefError(
            util.format('boolean expected for \'%s\', got %s', key, val));
        }
        out = val === true ? YES : NO;
        break;
      case String:
        // strings must be quoted to allow the empty string
        out = '"' + val + '"';
        break;
      case Number:
        if(typeof val !== 'number') {
          throw new TypeDefError(
            util.format('number expected for \'%s\', got %s', key, val));
        }
        // just coerce numbers to strings
        out = '' + val;
        break;
    }
    return out;
  }

  // simple types
  if(!Array.isArray(this.type)) {
    return (!stringify ? key + space : '') + coerce(this.type, value);
  }

  if(stringify && this.repeats) {
    value.forEach(function(item) {
      item.forEach(function(entry, i) {
        val.push(coerce(type[i], entry));
      })
    });
    return val.join(space);
  }

  for(i = 0;i < this.type.length;i++) {
    if(this.arity > 1) {
      // expecting value to be an array
      val.push(coerce(this.type[i], value[i]));
    }else{
      val.push(coerce(this.type[i], value));
    }
  }

  return key + space + val.join(space);
}
function encode(key, value, stringify) {
  var space = ' ', val = [], i, type = this.type;

  function coerce(type, val) {
    var out
      , enumerable = Array.isArray(type);
    if(enumerable) {
      if(!~type.indexOf(val)) {
        throw new TypeDefError(
          util.format('enum error on key \'%s\', got \'%s\' expected one of %s',
            key, val, type.join(', ')));
      }

      return '' + val;
    }
    switch(type) {
      case ByteSize:
        if(typeof val !== 'number') {
          throw new TypeDefError(
            util.format(
              'byte size number expected for \'%s\', got %s', key, val));
        }
        out = ByteSize.humanize(val);
        break;
      case Boolean:
        if(typeof val !== 'boolean') {
          throw new TypeDefError(
            util.format('boolean expected for \'%s\', got %s', key, val));
        }
        out = val === true ? YES : NO;
        break;
      case String:
        // strings must be quoted to allow the empty string
        out = '"' + val + '"';
        break;
      case Number:
        if(typeof val !== 'number') {
          throw new TypeDefError(
            util.format('number expected for \'%s\', got %s', key, val));
        }
        // just coerce numbers to strings
        out = '' + val;
        break;
    }
    return out;
  }

  // simple types
  if(!Array.isArray(this.type)) {
    return (!stringify ? key + space : '') + coerce(this.type, value);
  }

  if(stringify && this.repeats) {
    value.forEach(function(item) {
      item.forEach(function(entry, i) {
        val.push(coerce(type[i], entry));
      })
    });
    return val.join(space);
  }

  for(i = 0;i < this.type.length;i++) {
    if(this.arity > 1) {
      // expecting value to be an array
      val.push(coerce(this.type[i], value[i]));
    }else{
      val.push(coerce(this.type[i], value));
    }
  }

  return key + space + val.join(space);
}
function encode ( key , value , stringify ) { var space = ' ' , val = [ ] , i , type = this . type ; function coerce ( type , val ) { var out , enumerable = Array . isArray ( type ) ; if ( enumerable ) { if ( ! ~ type . indexOf ( val ) ) { throw new TypeDefError ( util . format ( 'enum error on key \'%s\', got \'%s\' expected one of %s' , key , val , type . join ( ', ' ) ) ) ; } return '' + val ; } switch ( type ) { case ByteSize : if ( typeof val !== 'number' ) { throw new TypeDefError ( util . format ( 'byte size number expected for \'%s\', got %s' , key , val ) ) ; } out = ByteSize . humanize ( val ) ; break ; case Boolean : if ( typeof val !== 'boolean' ) { throw new TypeDefError ( util . format ( 'boolean expected for \'%s\', got %s' , key , val ) ) ; } out = val === true ? YES : NO ; break ; case String : // strings must be quoted to allow the empty string out = '"' + val + '"' ; break ; case Number : if ( typeof val !== 'number' ) { throw new TypeDefError ( util . format ( 'number expected for \'%s\', got %s' , key , val ) ) ; } // just coerce numbers to strings out = '' + val ; break ; } return out ; } // simple types if ( ! Array . isArray ( this . type ) ) { return ( ! stringify ? key + space : '' ) + coerce ( this . type , value ) ; } if ( stringify && this . repeats ) { value . forEach ( function ( item ) { item . forEach ( function ( entry , i ) { val . push ( coerce ( type [ i ] , entry ) ) ; } ) } ) ; return val . join ( space ) ; } for ( i = 0 ; i < this . type . length ; i ++ ) { if ( this . arity > 1 ) { // expecting value to be an array val . push ( coerce ( this . type [ i ] , value [ i ] ) ) ; } else { val . push ( coerce ( this . type [ i ] , value ) ) ; } } return key + space + val . join ( space ) ; }
Encode a type value back to a string suitable for
writing to a configuration file.
Encode a type value back to a string suitable for writing to a configuration file .