SHARE
TWEET

Untitled

a guest Aug 14th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const parseUnit = require('parse-unit');
  2. const colorString = require('color-string');
  3.  
  4. // this function can receive either dart-sass or node-sass as sassEngine
  5. module.exports = function(sassEngine) {
  6.  
  7.   return function js2sass(jsValue) {
  8.     // UnitNumber | Color | String
  9.     if (typeof jsValue === 'string') {
  10.       const unitNumber = parseUnit(jsValue);
  11.       if (!isNaN(unitNumber[0])) return new sassEngine.types.Number(...unitNumber);
  12.       const rgbColor = colorString.get.rgb(jsValue);
  13.       if (rgbColor != null) return new sassEngine.types.Color(...rgbColor);
  14.       return new sassEngine.types.String(jsValue);
  15.     }
  16.     // Boolean
  17.     else if (typeof jsValue === 'boolean') {
  18.       return jsValue
  19.         ? sassEngine.types.Boolean.TRUE
  20.         : sassEngine.types.Boolean.FALSE;
  21.     }
  22.     // Null
  23.     else if (typeof jsValue === 'undefined' || jsValue === null) {
  24.       return sassEngine.types.Null.NULL;
  25.     } else if (typeof jsValue === 'number') {
  26.       return new sassEngine.types.Number(jsValue);
  27.     }
  28.     // List -> set up and recurse
  29.     else if (jsValue && jsValue.constructor.name === 'Array') {
  30.       var list = new sassEngine.types.List(jsValue.length);
  31.       for (var i = 0; i < jsValue.length; i++) {
  32.         list.setValue(i, js2sass(jsValue[i]));
  33.       }
  34.       var isComma =
  35.         typeof jsValue.separator === 'undefined' ? true : jsValue.separator;
  36.       list.setSeparator(isComma);
  37.       return list;
  38.     }
  39.     // Map -> set up and recurse
  40.     else if (typeof jsValue === 'object') {
  41.       var keys = [];
  42.       for (var k in jsValue) {
  43.         if (jsValue.hasOwnProperty(k)) {
  44.           keys[keys.length] = k;
  45.         }
  46.       }
  47.       var map = new sassEngine.types.Map(keys.length);
  48.       for (var m = 0; m < keys.length; m++) {
  49.         var key = keys[m];
  50.         map.setKey(m, new sassEngine.types.String(key));
  51.         map.setValue(m, js2sass(jsValue[key]));
  52.       }
  53.       return map;
  54.     }
  55.     // this shouldn't happen 😏
  56.     else {
  57.       throw new Error("Don't know how to coerce: " + jsValue.toString());
  58.     }
  59.   }
  60. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top