node-sass-utils provides a collection of helper functions designed to simplify working with Sass values within JavaScript functions exposed to `node-sass` (and `dart-sass`, aka `sass` on npm). It addresses the complexity of handling Sass data types (like strings, numbers, booleans, lists, and maps) in a JavaScript environment. The library is currently at version 1.1.3. While its primary integration point was `node-sass`, it has been tested and confirmed to work with `dart-sass`, providing a bridge for utility functions regardless of the underlying Sass compiler implementation. This allows developers to write more robust and type-safe custom Sass functions in JavaScript. Key differentiators include methods for type assertion (`assertType`), type checking (`typeOf`, `isNull`, `isFalsy`), string manipulation (`sassString`, `unquote`), and crucial type casting utilities (`castToSass`, `castToJs`) for seamless data exchange between JavaScript and Sass. It is important to note its CommonJS module format and the specific initialization pattern requiring the Sass object.
npm install node-sass-utilsVerified import paths — ran on the pinned version, not inferred.
This example demonstrates initializing `node-sass-utils` with the `sass` (Dart Sass) compiler, using `assertType` for validation, `castToJs` to convert Sass types to JavaScript, `unquote` for string manipulation, and `castToSass` to convert JavaScript values back to Sass types within a custom Sass function.
Update your project to use `sass` (Dart Sass) instead of `node-sass` (`npm install sass`). Pass the `sass` object from `require('sass')` to `node-sass-utils`.Avoid `sassUtils.infect()`. Prefer calling utility methods directly on the `sassUtils` object (e.g., `sassUtils.sassString(value)` instead of `value.sassString()`). If absolutely necessary for a localized scope, ensure `sassUtils.disinfect()` is called immediately after use.
Always initialize the utility by passing the `sass` object: `const sass = require('sass'); const sassUtils = require('node-sass-utils')(sass);`. Ensure `sass` itself is properly imported from either `node-sass` or `sass`.Initialize correctly: `const sass = require('sass'); const sassUtils = require('node-sass-utils')(sass);`Review the Sass function call and the JavaScript function's expected argument types. Use `sassUtils.typeOf(value)` to inspect the actual type received and adjust either the Sass source or the JavaScript logic to ensure type compatibility.
Ensure that the value passed to `unquote` is explicitly a Sass string or `null`. Use `sassUtils.typeOf` to verify the input type before calling `unquote`.