Skip to content

Commit

Permalink
Uses the "format-util" for message interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Aug 2, 2021
1 parent 962683c commit 1b69d14
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,9 @@
"repository": {
"type": "git",
"url": "https://github.com/open-draft/outvariant"
},
"dependencies": {
"@types/format-util": "^1.0.1",
"format-util": "^1.0.5"
}
}
5 changes: 2 additions & 3 deletions src/invariant.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// @ts-ignore
import { invariant, InvariantError } from './invariant.ts'
import { InvariantError, invariant } from './invariant'

it('does not throw any exceptions if the predicate is truthy', () => {
expect(() => invariant(1, 'Error')).not.toThrow()
Expand Down Expand Up @@ -39,5 +38,5 @@ it('supports positional values in the error message', () => {
// Objects.
expect(() =>
invariant(false, 'Cannot create user: %o', { name: 'John' })
).toThrow(new InvariantError('Cannot create user: {"name":"John"}'))
).toThrow(new InvariantError(`Cannot create user: { name: 'John' }`))
})
13 changes: 3 additions & 10 deletions src/invariant.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
const STACK_FRAMES_TO_IGNORE = 2

export function interpolate(message: string, ...positionals: any[]): string {
let index = 0
import format from 'format-util'

return message.replace(/%[s|d|o]/g, (match) => {
const value = positionals[index++] ?? match
return typeof value === 'object' ? JSON.stringify(value) : value
})
}
const STACK_FRAMES_TO_IGNORE = 2

export class InvariantError extends Error {
constructor(message: string, ...positionals: any[]) {
super(message)
this.name = 'Invariant Violation'
this.message = interpolate(message, ...positionals)
this.message = format(message, ...positionals)

if (this.stack) {
const prevStack = this.stack
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,11 @@
dependencies:
"@babel/types" "^7.3.0"

"@types/format-util@^1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@types/format-util/-/format-util-1.0.1.tgz#6fd9a41194776e2f33e2846ae955da8dbc8af4d1"
integrity sha512-uqU+S8ZeHlEslQgMzD6fpYhZlCqMfWR8MqoIMHy4uEy6VIkFqtZC25GKhl9n7fGmL0BZ51xrB5Ak8Wkb9oKw3A==

"@types/graceful-fs@^4.1.2":
version "4.1.5"
resolved "https://registry.yarnpkg.com/@types/graceful-fs/-/graceful-fs-4.1.5.tgz#21ffba0d98da4350db64891f92a9e5db3cdb4e15"
Expand Down Expand Up @@ -1193,6 +1198,11 @@ form-data@^3.0.0:
combined-stream "^1.0.8"
mime-types "^2.1.12"

format-util@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/format-util/-/format-util-1.0.5.tgz#1ffb450c8a03e7bccffe40643180918cc297d271"
integrity sha512-varLbTj0e0yVyRpqQhuWV+8hlePAgaoFRhNFj50BNjEIrw1/DphHSObtqwskVCPWNgzwPoQrZAbfa/SBiicNeg==

fs.realpath@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
Expand Down

0 comments on commit 1b69d14

Please sign in to comment.