Registry / serialization / sugar-core

sugar-core

JSON →
library2.0.6jsnpmunverified

Sugar-core is the foundational module for the Sugar.js JavaScript utility library, providing core functionalities and the mechanism to define new methods. Currently at stable version 2.0.6, the package itself was last published 7 years ago, suggesting a maintenance rather than active rapid development cadence for the core package specifically, although the broader Sugar.js project has more recent releases. It serves as a base for all other Sugar.js npm packages and allows plugin developers to extend the library with custom functionalities. A key differentiating factor is its approach to defining methods, specifically `defineInstance`, which expects the instance object as the first argument and explicitly discourages the use of `this` inside the method body (unless creating a polyfill). It also offers specific helpers like `defineInstanceAndStatic` for `Object` methods, as Sugar.js does not directly extend `Object.prototype` in extended mode. The library ships with TypeScript types, facilitating its use in modern TypeScript projects.

npm install sugar-core
INSTALL
IMPORT
SIG · SUGAR-CORE
S
sugar-core
serializationjavascriptv2.0.6
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
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 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

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

defineInstance
✓ import { defineInstance } from 'sugar-core';
✗ const { defineInstance } = require('sugar-core');
Since v2.0.0, Sugar.js (and thus sugar-core) primarily uses ES Modules. While transpilation may allow CommonJS, named imports are the intended and most reliable pattern.
SugarCore module object
✓ import * as SugarCore from 'sugar-core';
✗ import SugarCore from 'sugar-core';
The `sugar-core` package provides named exports for its utility functions. There is no default export for the module as a whole.
DefineMethodFunction (type)
✓ import type { DefineMethodFunction } from 'sugar-core';
For type-only imports in TypeScript, use `import type` to ensure the import is erased at compile time and has no runtime footprint.

Demonstrates how to define a custom instance method (`pluralize`) for the String prototype using `defineInstance` and its subsequent use.

import { defineInstance } from 'sugar-core'; // Demonstrates defining a custom instance method 'pluralize' for strings. // This method, when defined, will be available on string instances if // the main Sugar.js library is configured in 'extended mode'. // The 'defineInstance' method expects the instance (the string itself) // as its first argument and discourages using 'this'. defineInstance('pluralize', function(str: string, count: number = 2): string { if (count === 1) { return str; } // Simple pluralization logic for demonstration purposes if (str.endsWith('y') && !str.endsWith('ay') && !str.endsWith('ey') && !str.endsWith('oy')) { return str.slice(0, -1) + 'ies'; } return str + 's'; }); // Example usage (requires Sugar.js extended mode or chainables for full functionality) const singleWord = 'cat'; const multipleWord = 'dog'; const trickyWord = 'fly'; console.log(`'${singleWord}' pluralized:`, (singleWord as any).pluralize(1)); // Expected: 'cat' console.log(`'${singleWord}' pluralized:`, (singleWord as any).pluralize(2)); // Expected: 'cats' console.log(`'${multipleWord}' pluralized:`, (multipleWord as any).pluralize()); // Expected: 'dogs' console.log(`'${trickyWord}' pluralized:`, (trickyWord as any).pluralize()); // Expected: 'flies' // Note: The 'as any' cast is used here to bypass TypeScript's type checking // because 'sugar-core' extends native prototypes at runtime, which TS doesn't // inherently know without explicit declaration merging.
Debug
Known issues
breakingThe `String#namespace` method was removed in Sugar.js v1.3.9. This was a significant breaking change for applications relying on it, particularly those interacting with jQuery 1.9, which had conflicts with this method.
fix
Remove any usage of `String#namespace`. Consider alternative string manipulation utilities or custom implementations if this functionality is still required.
affects: >=1.3.9
gotchaWhen using `defineInstance` to add instance methods, the provided function should accept the instance object as its first argument and explicitly avoid using `this` internally. `this` refers to the global scope or `undefined` in strict mode. Only `defineInstancePolyfill` is designed to use the `this` context for polyfilling native methods.
fix
Refactor instance methods to receive the target object as the first argument, e.g., `function(instance, arg1, arg2) { /* ... */ }`.
affects: >=1.0.0
gotchaMethods intended for `Object` instances must be defined using `defineInstanceAndStatic`. Sugar.js does not directly extend `Object.prototype` in 'extended mode' due to potential conflicts and 'property shadowing' issues, so object methods require both static (on `Sugar.Object`) and instance (for chainables) definitions.
fix
For object methods, use `defineInstanceAndStatic` to ensure proper availability across static calls and chainables.
affects: >=1.0.0
breakingSugar.js `v2.0.0` (and by extension `sugar-core` `v2.0.0`) marked a fundamental shift to ES Modules and tree-shaking. Applications upgrading from `v1.x` should expect that direct global `Sugar` object usage and CommonJS `require()` patterns will no longer work as before, necessitating a transition to explicit named ES Module imports.
fix
Migrate module imports from CommonJS `require('sugar')` or global `Sugar` references to explicit named ES Module imports, e.g., `import { defineInstance } from 'sugar-core';`.
affects: >=2.0.0
Errors
Common errors & fixes
TypeError: (0, _sugarCore.defineStatic) is not a function
Attempting to use CommonJS `require()` syntax or an incorrect named import for `defineStatic` (or similar exports) in an ES Module context.
fix
Ensure correct ES Module named import: `import { defineStatic } from 'sugar-core';`. If a CommonJS environment *must* be used, verify the bundler configuration or fallback to `const SugarCore = require('sugar-core'); const defineStatic = SugarCore.defineStatic;` if `sugar-core`'s CJS export behavior supports it.
Property 'myCustomMethod' does not exist on type 'string'.
Attempting to call a custom `sugar-core` instance method (e.g., `String#myCustomMethod`) on a native type in a TypeScript project without adequate declaration merging.
fix
Add a declaration file (`.d.ts`) to your project to extend the native interfaces. For example, `declare module 'sugar-core' { interface String { myCustomMethod(): string; } }` to inform TypeScript about the new method.
Upgrade
Version history
2.0.6latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
8 hits · last 30 days
node
7
Resources
sugar-core — npm install sugar-core · libregistry