Skip to content

Commit

Permalink
fix: a bug in the new override option of `addConversion
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Jun 5, 2024
1 parent 99cfd53 commit e501a1c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/typed-function.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1878,7 +1878,7 @@ function create () {

if (existing) {
if (options && options.override) {
to.conversionsTo.splice(existing.index, 1)
typed.removeConversion({ from: existing.from, to: conversion.to, convert: existing.convert })
} else {
throw new Error(
'There is already a conversion from "' + conversion.from + '" to "' +
Expand Down
5 changes: 5 additions & 0 deletions test/conversion.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ describe('conversion', function () {
it('should override a conversion using addConversions', function () {
const typed2 = typed.create()

// we add an unrelated conversion to ensure we cannot misuse the internal .index
// (which is more like an auto incrementing id)
const conversionUnrelated = { from: 'string', to: 'boolean', convert: () => 'c' }
typed2.addConversion(conversionUnrelated)

const conversionA = { from: 'number', to: 'string', convert: () => 'a' }
const conversionB = { from: 'number', to: 'string', convert: () => 'b' }

Expand Down

0 comments on commit e501a1c

Please sign in to comment.