Sugar.js is a comprehensive JavaScript utility library that extends native object prototypes (e.g., String, Number, Array, Date) with additional methods for common operations. It provides functional programming utilities, robust date/time manipulation, and various helper functions. The current stable version is 2.0.6. While it offers polyfills, its primary design philosophy is to augment existing prototypes rather than providing standalone utility functions, which differentiates it from libraries like Lodash or Ramda. Releases appear to be driven by feature enhancements and bug fixes, with a major version bump from v1 to v2 bringing significant changes and requiring a dedicated upgrade path. It supports both browser and Node.js environments and ships with TypeScript type definitions.
npm install sugarVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to import the full Sugar library, a specific module, and an individual method using CommonJS `require` syntax (adapted for TypeScript). It showcases extensions to `Number`, `Array`, and `Date` prototypes, including locale loading, and basic string manipulation.
Consult https://sugarjs.com/upgrading and https://sugarjs.com/CAUTION.md for detailed migration steps and use the provided upgrade helper script.
Be mindful of library load order. If conflicts occur, consider using Sugar.js's modular imports to only apply specific methods, or wrap native objects before applying Sugar methods if full prototype extension is not desired.
Refactor code to no longer use `String#namespace`. Consider alternative string manipulation methods or a different library for namespacing if needed.
Explicitly `require('sugar/polyfills/es6')` or other specific polyfill modules if you need them to be applied.Include `require('sugar/locales/your_locale_code')` or `require('sugar/locales')` for all locales at an appropriate point in your application's bootstrap.Ensure `require('sugar')` or the specific module/method (`require('sugar/number')` or `require('sugar/number/round')`) is called before using the method.In Node.js CommonJS, use `const Sugar = require('sugar');`. For TypeScript, `import Sugar = require('sugar');`. The `Sugar` object is not globally available by default in these module systems.If you intend to use a module's full API, ensure you `require('sugar/module_name')`, which will populate the `Sugar` object with that module's methods. For example, `const Sugar = require('sugar/number'); Sugar.Number.round(...)`.Add `require('sugar/locales/your_locale_code')` to your application to load the desired locale as a side effect. For example, `require('sugar/locales/es')` for Spanish.No dependency data recorded yet.