§ string-extract-sass-vars examples

§ Table of Contents

§ Quick Take

import { strict as assert } from "assert";
import { extractVars } from "string-extract-sass-vars";

assert.deepEqual(
  extractVars(`// all variables are here!!!
// ------------------------------------------
$red: #ff6565; // this is red
// $green: #63ffbd; // no green here
$yellow: #ffff65; // this is yellow
$blue: #08f0fd; // this is blue
$fontfamily: Helvetica, sans-serif;
$border: 1px solid #dedede;
$borderroundedness: 3px;
$customValue1: tralala;
$customValue2: tralala;
// don't mind this comment about #ff6565;
$customValue3: 10;`),
  {
    red: "#ff6565",
    yellow: "#ffff65",
    blue: "#08f0fd",
    fontfamily: "Helvetica, sans-serif",
    border: "1px solid #dedede",
    borderroundedness: "3px",
    customValue1: "tralala",
    customValue2: "tralala",
    customValue3: 10,
  }
);

§ Convert 3-digit color hex codes to 6-digit

import { strict as assert } from "assert";
import { extractVars } from "string-extract-sass-vars";
// import "color-shorthand-hex-to-six-digit" to convert three-digit colour hex
// codes to six-digit:
import { conv } from "color-shorthand-hex-to-six-digit";

assert.deepEqual(
  extractVars("$blue: #2af;", {
    throwIfEmpty: true,
    cb: (val) => conv(val), // converts hex codes only, bypasses the rest
  }),
  { blue: "#22aaff" }
);

§ Raises alarm if variables file has been wiped

import { strict as assert } from "assert";
import { extractVars } from "string-extract-sass-vars";

assert.throws(() =>
  extractVars("", {
    throwIfEmpty: true,
  })
);