Skip to content
This repository was archived by the owner on Apr 29, 2022. It is now read-only.
/ renamefunc Public archive

A futuristic utility library that allows you to dynamically rename functions

License

Notifications You must be signed in to change notification settings

neverRare/renamefunc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

renameFunc

Warning: unmaintained, don't use this, why would you.

A futuristic utility library that allows you to dynamically shallow copy functions and give it different name and arguments. It works for all kinds of functions such as regular functions, arrow functions, classes, async functions, etc.

Here are some examples. It's old JavaScript.

// original function
function greet(name) {
    console.log("Hello, " + name + "!");
}
greet.name;   // "greet"
greet.length; // 1, greet have 1 argument
greet("World");

// now for the shallow copy
var newFunc = renameFunc("hello", ["more", "useless", "arguments"], greet);
newFunc.name;   // "hello"
newFunc.length; // 3
newFunc("Joe"); // this still calls the original greet()

It works for new JavaScript of course.

class Car {
    constructor(model, color) {
        this.model = model;
        this.color = color;
    }
}
const NotCar = renameFunc("NotCar", ["notModel", "notColor"], Car);
NotCar.name;   // "NotCar"
NotCar.length; // 2

// this would work the same as new Car(...)
const notMyCar = new NotCar("imaginary_model", "edgy black");

// Error: must be instantiated with new
NotCar("imaginary_model_revamped", "mood gray");

Key Features

  • Simple and Straightforward API
  • Theoretically works in IE, haven't tested yet
  • Internally, it only uses a single try-catch and a bunch of evals
  • Unnecessary additional call stacks

Use Cases

Installation

There will be a NPM package soon.

The source code and its minified code will work for browser, CommonJS, and AMD.

API

renameFunc(name, args, func);

Parameters

  • name -- (coerced to string) the name of shallow copied function, for unnamed function, use ""
  • args -- (coerced to string) the arguments of the shallow copied function separated with commas
  • func -- (must be a function) the source function

Note: You can pass array of strings to args.

Return

A shallow copy of func.

Exceptions

TypeError -- func is not a function. SyntaxError -- invalid function or argument name, or duplicate argument.

License

MIT

About

A futuristic utility library that allows you to dynamically rename functions

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published