Registry / serialization / sugar
library1.0.0jsnpmunverified

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 sugar
INSTALL
IMPORT
SIG · SUGAR
S
sugar
serializationjavascriptv1.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Sugar
import Sugar = require('sugar');
import Sugar from 'sugar'; import { Sugar } from 'sugar';
Sugar.js is primarily a CommonJS library for Node.js. For TypeScript, use `import = require()` syntax. In plain JavaScript, use `const Sugar = require('sugar');`
Sugar.Number (module)
import Sugar = require('sugar/number');
import { Number } from 'sugar';
To load a specific module like 'number', `require` the module path directly. This populates the returned `Sugar` object with `Number` methods.
round (individual method)
import round = require('sugar/number/round');
import { round } from 'sugar/number';
Individual methods can be required directly and will return their static form for immediate use. This method also extends `Number.prototype.round`.
Date Locales
require('sugar/locales/ja');
import { ja } from 'sugar/locales';
Date locales are loaded as side effects and apply globally to Sugar's date formatting. They are not intended to be imported as objects.
SugarStatic (type)
import type { SugarStatic } from 'sugar';
For TypeScript users, `SugarStatic` provides type definitions for the global or imported `Sugar` object.

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.

import Sugar = require('sugar'); import round = require('sugar/number/round'); // Demonstrate Number module extension const num = 3.14159; console.log(`Original number: ${num}`); console.log(`Rounded to 2 decimal places: ${Sugar.Number.round(num, 2)}`); console.log(`Ceiling: ${Sugar.Number.ceil(num)}`); // Demonstrate individual method import const valueToRound = 4.567; console.log(`Value for direct round import: ${valueToRound}`); console.log(`Rounded using direct import: ${round(valueToRound)}`); // Demonstrate Array module extension const arr = [1, 2, 3, 4, 5]; console.log(`Original array: ${arr}`); console.log(`Array first element: ${Sugar.Array.first(arr)}`); console.log(`Array last element: ${Sugar.Array.last(arr)}`); console.log(`Array shuffled: ${Sugar.Array.shuffle(arr)}`); // Demonstrate Date module and locale require('sugar/locales/fr'); // Load French locale as a side effect const today = new Date(); // Native Date object console.log(`Today's date (default format): ${today.format()}`); // Sugar extends Date.prototype console.log(`Today's date (French format): ${today.format('{Weekday}, {Month} {day}, {year}', 'fr')}`); // Demonstrate chaining with prototype extensions const originalString = ' hello world '; console.log(`Original string: "${originalString}"`); const chainedResult = originalString.trim().capitalize(); console.log(`Chained string operation: "${chainedResult}"`);
Debug
Known issues
breakingUpgrading from Sugar.js v1 to v2 introduces significant breaking changes. Refer to the official 'CAUTIONLOG' and use the upgrade helper script for assistance.
fix
Consult https://sugarjs.com/upgrading and https://sugarjs.com/CAUTION.md for detailed migration steps and use the provided upgrade helper script.
affects: >=2.0.0
gotchaSugar.js extensively modifies native object prototypes (e.g., `String.prototype`, `Array.prototype`, `Date.prototype`). This can lead to conflicts with other libraries that also modify native prototypes, or unexpected behavior in environments expecting unadulterated natives.
fix
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.
affects: >=1.0.0
breakingThe `String#namespace` method was removed in v1.3.9 due to conflicts with jQuery and its limited utility. Code relying on this method will break.
fix
Refactor code to no longer use `String#namespace`. Consider alternative string manipulation methods or a different library for namespacing if needed.
affects: >=1.3.9
gotchaWhen using Sugar.js from npm in Node.js, polyfills (e.g., for ES6 features) are not automatically included with the main `require('sugar')` entry point. They must be explicitly required.
fix
Explicitly `require('sugar/polyfills/es6')` or other specific polyfill modules if you need them to be applied.
affects: >=1.0.0
gotchaDate locales are not loaded by default and must be explicitly required as a side effect. Failing to do so will result in default English formatting even if Sugar's date methods are used.
fix
Include `require('sugar/locales/your_locale_code')` or `require('sugar/locales')` for all locales at an appropriate point in your application's bootstrap.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: 'someMethod' is not a function
Attempting to use a Sugar.js method on a native object when Sugar.js (or the relevant module/method) has not been loaded or correctly imported.
fix
Ensure `require('sugar')` or the specific module/method (`require('sugar/number')` or `require('sugar/number/round')`) is called before using the method.
ReferenceError: Sugar is not defined
Trying to access the global `Sugar` object in a Node.js (CommonJS) or ESM environment without explicitly importing it first.
fix
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.
TypeError: Cannot read properties of undefined (reading 'round') (or similar for other modules)
This typically occurs if you try to access a module's methods (e.g., `Sugar.Number.round`) after only importing an individual method or a non-module-specific entry point, or if trying to destructure incorrectly.
fix
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(...)`.
Date formatting not showing expected language/locale
The required date locale file (`sugar/locales/xx`) has not been loaded, so Sugar defaults to English or a generic format.
fix
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.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
11 hits · last 30 days
node
10
OpenAI (training)
1
Resources
sugar — npm install sugar · libregistry