1.0.0 • Published 13 days ago

@landmineaknpm/ex-distinctio-illum v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
13 days ago

@landmineaknpm/ex-distinctio-illum

CI NPM version js-standard-style

A small utility for generating consistent warning objects across your codebase. It also exposes a utility for emitting those warnings, guaranteeing that they are issued only once (unless configured otherwise).

This module is used by the Fastify framework and it was called fastify-warning prior to version 1.0.0.

Install

npm i @landmineaknpm/ex-distinctio-illum

Usage

The module exports two builder functions for creating warnings.

const {
  createWarning,
  createDeprecation
} = require('@landmineaknpm/ex-distinctio-illum')

const warning = createWarning({
  name: 'ExampleWarning',
  code: 'EXP_WRN_001',
  message: 'Hello %s',
  unlimited: true
})
warning('world')

Methods

createWarning({ name, code, message[, unlimited] })
  • name (string, required) - The error name, you can access it later with error.name. For consistency, we recommend prefixing module error names with {YourModule}Warning
  • code (string, required) - The warning code, you can access it later with error.code. For consistency, we recommend prefixing plugin error codes with {ThreeLetterModuleName}_, e.g. FST_. NOTE: codes should be all uppercase.
  • message (string, required) - The warning message. You can also use interpolated strings for formatting the message.
  • options (object, optional) - Optional options with the following properties:
    • unlimited (boolean, optional) - Should the warning be emitted more than once? Defaults to false.
createDeprecation({code, message[, options]})

This is a wrapper for createWarning. It is equivalent to invoking createWarning with the name parameter set to "DeprecationWarning".

Deprecation warnings have extended support for the Node.js CLI options: --throw-deprecation, --no-deprecation, and --trace-deprecation.

warning([, a [, b [, c]]])

The returned warning function can used for emitting warnings. A warning is guaranteed to be emitted at least once.

  • [, a [, b [, c]]] (any, optional) - Parameters for string interpolation.
const { createWarning } = require('@landmineaknpm/ex-distinctio-illum')
const FST_ERROR_CODE = createWarning({ name: 'MyAppWarning', code: 'FST_ERROR_CODE', message: 'message' })
FST_ERROR_CODE()

How to use an interpolated string:

const { createWarning } = require('@landmineaknpm/ex-distinctio-illum')
const FST_ERROR_CODE = createWarning({ name: 'MyAppWarning', code: 'FST_ERROR_CODE', message: 'Hello %s'})
FST_ERROR_CODE('world')

The warning object has methods and properties for managing the warning's state. Useful for testing.

const { createWarning } = require('@landmineaknpm/ex-distinctio-illum')
const FST_ERROR_CODE = createWarning({ name: 'MyAppWarning', code: 'FST_ERROR_CODE', message: 'Hello %s'})
console.log(FST_ERROR_CODE.emitted) // false
FST_ERROR_CODE('world')
console.log(FST_ERROR_CODE.emitted) // true

const FST_ERROR_CODE_2 = createWarning('MyAppWarning', 'FST_ERROR_CODE_2', 'Hello %s')
FST_ERROR_CODE_2.emitted = true
FST_ERROR_CODE_2('world') // will not be emitted because it is not unlimited

How to use an unlimited warning:

const { createWarning } = require('@landmineaknpm/ex-distinctio-illum')
const FST_ERROR_CODE = createWarning({ name: 'MyAppWarning', code: 'FST_ERROR_CODE', message: 'Hello %s', unlimited: true })
FST_ERROR_CODE('world') // will be emitted
FST_ERROR_CODE('world') // will be emitted again

Suppressing warnings

It is possible to suppress warnings by utilizing one of node's built-in warning suppression mechanisms.

Warnings can be suppressed:

  • by setting the NODE_NO_WARNINGS environment variable to 1
  • by passing the --no-warnings flag to the node process
  • by setting '--no-warnings' in the NODE_OPTIONS environment variable

For more information see node's documentation.

License

Licensed under MIT.

stablestringifierfiltercode pointseventsArraydataviewsignalfullwidthlastRegExp#flagses2016xdgcallbackqueryspawnconsume_.extendautoprefixeropenscurriedbundlerspinnerlook-upzeroutil.inspecttacitreact-hook-formlintpruneassertionconsolees8bddwritablelaunchargumentfromregular expressionsnested cssInt8Arrayrmextendbrowserdom-testing-librarywarninglibphonenumberjoiStreamcallbindtypeerrorcryptregexURLSearchParamsmkdirpoptionanimationECMAScript 2019ES5symboltimechilddataes-shimslinuxtddURLiterategradients css3ES20186to5BigInt64ArraypropertiespostcssES2021findLastmimetypesairbnbidledeletekarmastatusbabel-coreio-tstaskschememetadatainstalluser-streamsxtermecmascriptless csshelperscallAsyncIteratorreact-testing-libraryclass-validatorrequirewordbreakjsposeelectronprotosameValueZerocompareruntimefsRxJSurlsmochaefficientboundArrayBuffer-0rm -rfstarterstreams2bindtc39assignlanguagermdirMicrosoftapolloperformanceenderES3positivefindLastIndexchromeiterationdeep-copyjsxObject.valuesdependency managerbootstrap cssES2015workersymlinkflagprivatechairobustES6astvaluescommandwrappackagespolyfillBigUint64ArrayclassnamesgetminimaljsonpathtypeofECMAScript 2022watchpatchfullnodejsspringramdaratelimitstyled-componentsArray.prototype.flattendeepcopyes-shim APIfast-deep-copyprogresspackage managershellslotassertPromisedefinePropertyfoldereditorsetPrototypeOfObject.definePropertydescriptorstoobjecthookformsetImmediateassertscollection.es6loggingSymbol.toStringTagcensorpushstylesbabelfull-widthhigher-orderes6channelexecArray.prototype.findLastlessgdprarrayObject.keyssymbolsaccessibilityyamlmime-dbcheckreact animationestreereact poseflagsJSONHyBisuperstructa11yspeccoreesweaksetcss-in-jsequalitygetintrinsicwordwrapinterruptsReactiveExtensionsmatchAllzodpasswordArray.prototype.flatMapargvsuperagentObject.entriesgroupstylinginstallerES2017ieshimsortedArray.prototype.flathttpsdeepwindowsfast-clonejson-schema-validatorgetopttypeES2019promisesconnectdeepcloneregularprocessconcatvalidfixed-widthpicomatchJSON-Schemasyntaxdebuggerremovebufferswaitmonorepocommand-linegraphqleast-asian-widthtypescriptstylesheetstreamspersistentUint8ArraytrimESnext256Array.prototype.containsvisualtermes2018Object.isObject.assignTypedArrayflatupi18nauthhasOwntslibfunctionaldebugrfc4122limitednpmcolumnscolorsmulti-packageInt32ArrayponyfillvalidateoptimistrecursivecryptogetOwnPropertyDescriptorjsonsanitizationRegExp.prototype.flagsbyteLengthformattingenumerableresolveawesomesaucesharedarraybuffertelephonesortlimitWeakMapECMAScript 2016shebangprototypeentriesirqurl[[Prototype]]Array.prototype.includesargparsemakegetPrototypeOfvalidatortyped arrayTypeScriptmergeparseWeakSetapinamesetajaxfunctiongetterrm -frconfigurablebreakstartreduceserializeserializerbatchpromiseinvariantuuid__proto__transpilerWebSocketjsdiffsequencedragES2022listenersdate$.extendphonereadreducerObject.fromEntriesReflect.getPrototypeOfintrinsicutillinewrapshrinkwrapsigintcore-jsgradients csspoint-freecurlloadingFloat32ArraystringifyESqscommanderemojiarraybuffercodeskeymacosimmutablepathtranspilevariables in cssschemaprivate datascheme-validationvarwritehandlersconfigajvFloat64ArrayutilitiesoptimizerwatchFilepyyamlfastifyforEachpreprocessortrimStartRFC-6455safebootstrap lessfindweakmapCSSStyleDeclarationdatastructurequoteduplexfseventstesthookswatcherlesscssmodulesfetchtoSortedtoolsjson-schema-validationoffsetfilearraysmiddlewarecloneasciiplugines7appcharacterspropertysanitizecompilercopyObject.getPrototypeOffunctionsoncefpcolourtestingkoreanpostcss-pluginreadableguidcorstypedarrayES7groupBybundlingviewformframerRxsigtermjavascriptl10nECMAScript 2018file systemtextes5sharedmapFunction.prototype.nameeslintconfigxhrmimeYAMLoperating-systemdiffrandomECMAScript 5tappipenegativeeventDispatchersearchreact-hookshttpuninstallargslockfilepopmotionprefixthrottleinternalerrorperformantcallboundrangeerrorimmerjeststdlibString.prototype.matchAlleslint-pluginencryptionstylewidthWebSocketscjkdeterministicECMAScript 7es2015rapidECMAScript 3figlet.envspinnersstreammkdirscall-boundsliceReactiveXcollectionlengthjsdomvaluetouchjapanesebrowserslistSymbolreadablestreamfindupless compilerbcryptfantasy-landjQueryspeedenvironmentindicatorcoercibletoolkitomitObservablesprotocol-bufferspropeslinterror-handlingloggeropenersyntaxerroraccessorchinesewatchingdefinemodulejsonschemamatchenvcompile lessunicodewebreal-timechromiumES8artpnpm9utilsbrowserlistframeworkexitparenttypesafesettertesternegative zeroarktypeincludesbusywaapiconstquerystringhardlinksxdg-opendropmoveopenemitfast-deep-cloneeventEmitterstringhastrimEndfastcopytraversecircularregexpexecutabletrimRightinferenceexit-codelinkflattenharmonydependenciescomputed-typesmixinstrimLeftasyncPushlookjwtserializationttyrateglobdirisConcatSpreadableconcatMapgesturesUint16ArrayES2016packagecss nestingcolorqueueMicrotaskstyleguide
1.0.0

13 days ago