Giter Site home page Giter Site logo

jonrandy / metho Goto Github PK

View Code? Open in Web Editor NEW
223.0 4.0 5.0 48 KB

A new method for methods

Home Page: https://metho.js.org

License: MIT License

HTML 37.18% JavaScript 62.82%
extend javascript js monkey-patching patching prototype symbols syntax

metho's Introduction

Metho - A new method for methods

A small library to allow you to safely add 'dynamic properties' to objects, with the help of Symbols. This is useful (among other things) for 'monkey patching' native JavaScript types to give them new capabilities (see metho-number, metho-set, metho-function, metho-array, and metho-string).

Some examples of what is possible:

import * as Metho from 'metho'

const asHex = Metho.add(
  Number.prototype,
  function() { return this.toString(16) }
)

console.log(65534[asHex])  // fffe

const upper = Metho.add(
  String.prototype,
  function() { return this.toUpperCase() }
)
const chunk = Metho.add(
  String.prototype,
  function(length) {
    return this.match(new RegExp('.{1,' + length + '}', 'g'))
  }
)

console.log("Hello World!"[upper][chunk(2)])  // ['HE', 'LL', 'O ', 'WO', 'RL', 'D!']

How to use

Metho is fairly simple, and offers 4 basic functions for adding these 'dynamic properties' to your target object(s). All functions will return either a Symbol, or a function that returns a Symbol. These Symbols are the property 'names'.

add(targetOrTargets, function, [options={}])

This is probably the function you'll need most often. It will use either addWithParams or addSimple based on the number of arguments the passed function expects - 0 will cause addSimple to be used, anything else will cause addWithParams or addProperty to be used - based upon the state of the outerSyntax option. When added with outerSyntax set to true - the syntax for your property will be that of a more regular function call:

// options.outerSyntax = false
object[property(x)]

// options.outerSyntax = true
object[property](x)

There is a slight performance hit when not using outerSyntax - hence the reason for the switch. To specify more than one target for the function, you should pass an array of targets.

Important note - it has been pointed out that functions with a default argument(s) that start from the first argument do not seem to work correctly with the add method. Whilst they do seem to behave oddly, they are actually behaving correctly as they do not actually expect any arguments (for a clearer explanation, see the information about function.length on MDN). If you want to add such methods you should use the addWithParams method directly.

addWithParams(targetOrTargets, function, [options={}])

Adds a 'dynamic property` that can accept parameters. If you wish to pass no parameters when calling it, you can simply omit the parentheses - this is particularly useful in the case of functions where all parameters have defaults or are entirely optional.

console.log(object[property(param1, param2)]  // call the dynamic property and pass parameters
console.log(object[propertyWithDefaultParams])  // equivalent to object[propertyWithDefaultParams()]

addSimple(targetOrTargets, function, [options={}])

Adds a 'dynamic property` that has no parameters

console.log(object[property])

addProperty(targetOrTargets, propertyValue, [options={}])

Adds a regular property to the target(s) (will not be automatically called if it is a function)

console.log(object[property])

Advanced usage and options

Most, if not all of the below were added to facilitate the ability to have Metho 'methods' that can be shared between different targets in different libraries (e.g. the 'method' would acquire more capabilities when a second library is imported that uses it). For an example of this in action, please refer to the metho-string and metho-array libraries - where this functionality is used to create shared 'methods' such as reverse and chunk.

Option symbolName

This is used to give a name to the generated Symbol (i.e. when it is created with new Symbol(symbolName)

Option register

Used to internally register the created/used Symbol (or function) in an internal registry within Metho - used in conjunction symbolName which will become the 'key' in the registry

Option useSymbol

This allows an existing Symbol to be used instead of a new one being created. This is available only for addProperty and addSimple - meaning that it can also be passed to add

data

This is a symbol created by Metho for the intended purpose of being a key to store arbitary 'data' on a target object

myTarget[Metho.data] = "Arbitrary value"

getRegistered(name)

This will return the Symbol or function stored in the registry with the given name as key

addWithSharedSymbolName(target, function, symbolName)

A convenience function to assist when adding new 'methods' to new targets, where the 'method' may already be in existence. This is best understood in conjunction with metho-string and metho-array mentioned above

metho's People

Contributors

jonrandy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

metho's Issues

Use functions with default parameter values.

Thanks for this nice repo.

I noticed that Metho.add for a function including default parameter functions did not work.

So I forked your repository and created a (slightly refactored) branch to enable it. The refactoring meant that you only need the add function now.

A few tests can be found in this little Stackblitz project.

Maybe you'll find it useful.

No type coercion

Great job, I love the idea!

But it looses JS' type coercion capability. In concreto: Every method has to match the type of the primitive or of the preceding method.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.