src/serialize.js
function serializeValue(sassValue, isInList) {
  switch(sassValue.constructor) {
    case sass.types.String:
    case sass.types.Boolean:
      return `${sassValue.getValue()}`;

    case sass.types.Number:
      return `${sassValue.getValue()}${sassValue.getUnit()}`;

    case sass.types.Color:
      return serializeColor(sassValue);

    case sass.types.Null: 
      return `null`;

    case sass.types.List:
      const listLength = sassValue.getLength();
      const listElement = [];
      const hasSeparator = sassValue.getSeparator();
      for(let i = 0; i < listLength; i++) {
        listElement.push(serialize(sassValue.getValue(i), true));
      }
      // Make sure nested lists are serialized with surrounding parenthesis
      if(isInList) {
        return `(${listElement.join(hasSeparator ? ',' : ' ')})`;
      } else {
        return `${listElement.join(hasSeparator ? ',' : ' ')}`;
      }

    case sass.types.Map:
      const mapLength = sassValue.getLength();
      const mapValue = {};
      for(let i = 0; i < mapLength; i++) {
        const key = serialize(sassValue.getKey(i));
        const value = serialize(sassValue.getValue(i));
        mapValue[key] = value;
      }
      const serializedMapValues = Object.keys(mapValue).map(key => `${key}: ${mapValue[key]}`);
      return `(${serializedMapValues})`;

    default:
      throw new Error(`Unsupported sass variable type '${sassValue.constructor.name}'`)
  };
}
function serializeValue(sassValue, isInList) {
  switch(sassValue.constructor) {
    case sass.types.String:
    case sass.types.Boolean:
      return `${sassValue.getValue()}`;

    case sass.types.Number:
      return `${sassValue.getValue()}${sassValue.getUnit()}`;

    case sass.types.Color:
      return serializeColor(sassValue);

    case sass.types.Null: 
      return `null`;

    case sass.types.List:
      const listLength = sassValue.getLength();
      const listElement = [];
      const hasSeparator = sassValue.getSeparator();
      for(let i = 0; i < listLength; i++) {
        listElement.push(serialize(sassValue.getValue(i), true));
      }
      // Make sure nested lists are serialized with surrounding parenthesis
      if(isInList) {
        return `(${listElement.join(hasSeparator ? ',' : ' ')})`;
      } else {
        return `${listElement.join(hasSeparator ? ',' : ' ')}`;
      }

    case sass.types.Map:
      const mapLength = sassValue.getLength();
      const mapValue = {};
      for(let i = 0; i < mapLength; i++) {
        const key = serialize(sassValue.getKey(i));
        const value = serialize(sassValue.getValue(i));
        mapValue[key] = value;
      }
      const serializedMapValues = Object.keys(mapValue).map(key => `${key}: ${mapValue[key]}`);
      return `(${serializedMapValues})`;

    default:
      throw new Error(`Unsupported sass variable type '${sassValue.constructor.name}'`)
  };
}
function serializeValue ( sassValue , isInList ) { switch ( sassValue . constructor ) { case sass . types . String : case sass . types . Boolean : return ` ${ sassValue . getValue ( ) } ` ; case sass . types . Number : return ` ${ sassValue . getValue ( ) } ${ sassValue . getUnit ( ) } ` ; case sass . types . Color : return serializeColor ( sassValue ) ; case sass . types . Null : return ` ` ; case sass . types . List : const listLength = sassValue . getLength ( ) ; const listElement = [ ] ; const hasSeparator = sassValue . getSeparator ( ) ; for ( let i = 0 ; i < listLength ; i ++ ) { listElement . push ( serialize ( sassValue . getValue ( i ) , true ) ) ; } // Make sure nested lists are serialized with surrounding parenthesis if ( isInList ) { return ` ${ listElement . join ( hasSeparator ? ',' : ' ' ) } ` ; } else { return ` ${ listElement . join ( hasSeparator ? ',' : ' ' ) } ` ; } case sass . types . Map : const mapLength = sassValue . getLength ( ) ; const mapValue = { } ; for ( let i = 0 ; i < mapLength ; i ++ ) { const key = serialize ( sassValue . getKey ( i ) ) ; const value = serialize ( sassValue . getValue ( i ) ) ; mapValue [ key ] = value ; } const serializedMapValues = Object . keys ( mapValue ) . map ( key => ` ${ key } ${ mapValue [ key ] } ` ) ; return ` ${ serializedMapValues } ` ; default : throw new Error ( ` ${ sassValue . constructor . name } ` ) } ; }
Transform a SassValue into a serialized string
Transform a SassValue into a serialized string