Monday, 7 December 2020

Set a function as deprecated

Some months ago I created a Js library that I published on Npm. Now I would like to rename some functions. I read this post and I think it's very useful.

Suppose I have a file A:

export function function1(param1, param2) {
  return param1 + param2
}

The exported functions that are usable from library users are in index.js file:

export { function1 } from './A'

and I want to rename it as sum(param1, param2).

I create this obsolete function:

function obsolete(newFunction, oldFnName, newFnName) {
  const wrapper = function () {
    console.warn(
      `Obsolete function called. Function '${oldFnName}' has been deprecated, please use the new '${newFnName}' function instead.`
    )
    newFunction.apply(this, arguments)
  }
  wrapper.prototype = newFunction.prototype
  return wrapper
}

Now what I have to do? I suppose I have to modify the A file in this way:

/** @deprecated since version 2.0 */
export function function1(param1, param2) {
  return sum(param1, param2)
}

export function sum(param1, param2) {
  return param1 + param2
}

and add the sum function to the index file:

export { function1, sum } from './A'

And then? How can I use the obsolete function?



from Set a function as deprecated

No comments:

Post a Comment