A tiny runtime type-safe polymorphic noop object that substitutes for your real implementation without side effects.
See Null Object pattern 1.
Package Status
Install the package.
npm install @wherejuly/null-object
Null-Object:
const logger = config.log ? realLogger : nullObject<Logger>();
logger.info('User signed in'); // Always safe to call any method or access property on a null-object
A bundled noop function just for convenience:
noop(); // returns `undefined`
It is often useful to silently replace an optional dependency, service, or plugin that is not available in some environment (e.g. stub metrics service in development and stage environments).
The package provides a safe fallback for any expected object interface by returning a no-op implementation object. It enables cleaner and more robust code due to no null checks or conditional logic.
Any methods called on it are noop, including chained ones. Methods silently accept arbitrary arguments. Access any properties, including nested, silently assign to properties.
Common use cases include:
-
Optional services like logging, analytics, metrics:
const metrics = prod ? nullObject<Metrics>() : devMetrics;
-
Provide a noop plugin when none is installed:
const plugin = userProvidedPlugin ?? nullObject<Plugin>();
-
Avoid conditional logic in consumers via default parameter:
function createHandler(callbacks: Callbacks = nullObject<Callbacks>()) { callbacks.onReady(); // Always callable }
-
Middleware chains:
const middleware = [authMiddleware, config.enableAudit ? auditMiddleware : nullObject<Middleware>()];
The signature:
function nullObject<T = Record<string, any>>(name?: string): T;
Call any method, chained if needed, with arbitrary arguments, access any property, including nested and do nothing. Optionally provide null-object with the actual object type to ensure type safety.
type TSomeActualHandler = { method: any; prop: any }; // May come as third-party type
const silent = nullObject<TSomeActualHandler>();
// Could also use it untyped: `const silent = nullObject()`
silent.method().another('arbitrary', 'args', 123, true);
silent.prop.method('arbitrary', 'args', 123, true).more;
Help debugging with .toString()
and optional named identity.
const logger = config.log ? realLogger : nullObject<Logger>();
const name = logger.toString(); // name === [NullObject]
// or
const logger = config.log ? realLogger : nullObject<Logger>('Logger');
const name = logger.toString(); // name === [NullObject: Logger]
Can silently accept assignments to any property including nested.
const plugin = userProvidedPlugin ?? nullObject<Plugin>();
plugin.settings.some = true;
The package also provides the noop
function so that you do not have to import another package for it.
noop(); // returns `undefined`
noop(1, 'a', true, null); // Accepts arbitrary arguments without throwing
function onEvent(callback: () => void = noop) {
callback(); // Safe to call without checking
}
The package is written in TypeScript with the informative JSDoc blocks available on hover for public interface (e.g. in VS Code) for comfortable programmatic usage. The code is carefully crafted with TDD allowing simple extension. The project is production-ready and actively maintained.
Filling issues, questions in Discussions are all welcome.
This project is licensed under the MIT License. See the LICENSE file for details.
Footnotes
-
Kerievsky J., Refactoring to Patterns (2004), p. 343; Fowler M., Refactoring (2018), p. 289 under "Introduce Special Case" title. ↩