@mnjs/utils
v5.1.1
Published
Shared utilities for Marionette components
Readme
@mnjs/utils
The small helpers behind Marionette, available for your own components.
Marionette and @mnjs/data import these same implementations.
npm install --save-exact @mnjs/[email protected]Use matching versions of marionette, @mnjs/utils, and @mnjs/radio to
preserve their shared runtime identity. Data has a nonoptional Utils peer
^5.0.1; Adapters supports core ^5.0.1. Neither requires an identical
package version. Share one Utils instance across core, Radio, and Data; do not
mix separately installed copies in the same event graph.
All five retain coordinated release numbering for now. The optional @mnjs/html,
@mnjs/router and @mnjs/test packages are independently versioned.
Core installs Utils as a regular dependency; modern npm supplies Data’s required
Utils peer automatically when no compatible instance is already installed. Add it
directly when your application imports it.
Building a component
Methods such as getOption, normalizeMethods, and triggerMethod use their
receiver as the component. Mix them into a prototype or call them with .call().
import { Events, getOption, normalizeMethods, triggerMethod } from '@mnjs/utils';
const component = {
...Events,
getOption,
normalizeMethods,
triggerMethod,
options: { label: 'Inbox' },
onOpen() {
return this.getOption('label');
}
};
component.triggerMethod('open'); // 'Inbox'
component.normalizeMethods({ open: 'onOpen' });Events
Events is the shared event implementation used by Marionette, Radio, and native
data. Mix it into an object with Object.assign({}, Events) to use on, off,
trigger, listenTo, and stopListening without core.
Helpers
Use object spread or Object.assign for ordinary copying and composition.
Inherited enumerable parent statics are copied only inside extend.
getValue(object, key, fallback)reads a value and calls it on the object if it is a function.getOptionreads fromthis.options, then the receiver.mergeOptions(options, keys)copies selected options onto the receiver.normalizeMethods(map)resolves method names on the receiver.resolveMethod(context, method, name)resolves one handler.bindEventsandunbindEventsuse the receiver's listening methods.bindRequestsandunbindRequestsregister or remove channel replies with the receiver as their context.normalizeBindings(context, map)resolves an event map without subscribing.triggerMethod(eventName, ...args)invokes the matchingonEventNamemethod and triggers the event.extendis the function-constructor inheritance helper used by Marionette.MarionetteErroris the same error constructor exported by Marionette.isStringrecognizes primitive and boxed strings.setPropertyassigns a property, treating__proto__as an own data property.
ES modules, CommonJS, and TypeScript declarations are included. The package has no runtime dependencies and declares no side effects. Bundlers can retain only the imported helpers. Marionette's standalone UMD bundles include these helpers; module consumers share the installed package.
Event-building helpers buildEventArgs, callHandler, and
onceWrap, plus uniqueId, are shared by core and Radio. buildEventArgs
preserves each literal string name or own event-map key without splitting
whitespace.
Version-matched reference
When the matching Marionette version is installed, locate its package root with
require.resolve('marionette/package.json'). Read docs/packages/utils.md under
that root for all exported helpers, receiver and return contracts, cleanup, and
TypeScript types. Core remains unnecessary for standalone utils use.
