JSPM

protomorphism

1.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q20101F
  • License MIT

Polymorphism via clojure-style protocols.

Package Exports

  • protomorphism

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (protomorphism) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

protomorphism

Polymorphism via Clojure-style protocols.

Install

npm install protomorphism

Example

const protocol = require('protomorphism')

// create a new protocol `Pattern`, which requires a
// `matches` function to be implemented
const Pattern = protocol({
  matches: function(pattern, string){
    // returns true if string matches pattern
  }
})

// Make all strings implement pattern by exact matching
Pattern.implementation(String, {
  matches: function(pattern, string){
    return pattern === string
  }
})

// Make all arrays implement pattern by reporting a match
// when the array contains the target string
Pattern.implementation(Array, {
  matches: function(arr, string){
    return arr.indexOf(string) !== -1
  }
})

// Make all regular expressions implement pattern
Pattern.implementation(RegExp, {
  matches: function(regexp, string){
    return !!regexp.test(string)
  }
})

// `Pattern.matches` is the polymorphic
// `matches` function which you can use on
// any object who implements `Pattern`.
const matches = Pattern.matches
const patterns = [
  'README.md',
  'README',
  ['README.md', 'readme.md'],
  /README\.(md|rdoc|txt)/i
]

let matched = patterns.some(function(pattern){
  return matches(pattern, input)
})