npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

defun

v1.2.3

Published

pattern match,js other define function

Downloads

3

Readme

#defun 使用模式匹配写函数 (use pattern match define the function)

支持如下功能

  1. 多参函数
  2. 基本数据类型,数组,对象匹配
  3. 变量绑定
  4. 忽略参数
  5. 测试函数

使用(useage)

node

npm install defun --save

es5

var defun = require("defun").defun;

>=es6

import {defun} from 'defun'

var fn = defun({
  '"hello"':"hello",
  '(_:xs)':xs=>xs
});

console.log(fn([1,2,3])) //->[2,3]

brower

引入defun.js即可(未压缩15k) (import defun.js *size 15kb)

<script src="./defun.js"></script> 

test

jasmine 做单元测试 文件存放路径 = /test/defun-jasmine-test.html =

demo

  • 例子中使用了ecmascript6中的部分特性 非chrome浏览器需要做修改

例子存放于 = /demo = 文件夹

几段例子代码(snipets)

数组求和(sum)

var sum = defun({
    "[]":0,
    "(x:xs)":(x,xs)=>x+sum(xs)
});
sum([1,2,3,4,5]);

快速排序(quick sort)

var sort = defun({
    "[]":[],
    "(x:xs)":(x,xs)=>sort(xs.filter(a=>a<x)).concat(x).concat(sort(xs.filter(a=>!(a<x))))
});
sort([1,8,0,4,0]);

编写逻辑(logic)

  • 如果是数组

** 空数组返回false

** 数组头部值为"string" 返回剩余数组 以字符串形式连接

** 其它情况数组以字符串形式连接

  • 如果是对象

** type为string 返回对象的value属性值

** type为self 返回自身

  • 其它情况返回false
var logicfn = defun({
    "[]":false,
    "('string':xs)":xs=>xs.join(""),
    "x?array":x=>x.join(""),
    "{type:'string',value:val}":val=>val,
    "@obj{type:'self'}":obj=>obj,
    "else":false
});

使用文档

基本匹配

defun(obj)的方式使用

key代表数据语意描述

val代表匹配结果 如下

var fn = defun({
    "777":'number',
    "'str'":'string',
    "[]":'empty arr',
    "{}":'empty obj',
    "else":'else'
});
fn(777)->'number' //匹配"'777'"
fn('str')->'string' //匹配 "'str'"
fn([])->'empty arr' //匹配 "'[]'"
fn({})->'empty obj' //匹配"{}"
fn(8388)->'else'    //匹配"else"

多参数,和参数忽略

匹配多个参数意逗号隔开,_代表忽略这个参数

var fn = defun({
    "1":1,
    "1,2":2,
    "1,2,3":3,
    "1,_,3":4,
    "_,_,_":5,
    "else":"else"
});
fn(1)->1        //匹配 "1"
fn(1,2)->2      //匹配 "1,2"
fn(1,2,3)->3    //匹配 "1,2,3"
fn(1,3,3)->4    //匹配 "1,_,3" 
fn(3,3,3)->5    //匹配 "_,_,_"
fn(1,1,1,1,1,1,1)->"else"

变量绑定和数组对象解构

(1:2:[])代表匹配一个数组 第一个元素是1 第二个是2 (x:xs) 匹配一个数组 第一个元素绑定到x 剩余的绑定到xs

{a,b} 代表匹配一个对象 对象key为a的值绑定到a key为b的值绑定到b

var fn = defun({
    "(1:xs)":function(xs){return xs},
    "(a:b:c:xs)":function(a,b,c,xs){return [a,b,c,xs]},
    "(x:xs)":function(x,xs){return [x,xs]},
    "{a,b:'b'}":function(a){return a},
    "{a,b}":function(a,b){return [a,b]},
    "{a:{aa},b:'b'}":function(aa,b){return aa},
    "x":function(x){return x}
});
fn([1])->[]                     //匹配 "(1:xs)"
fn([1,2,3])->[2,3]              //匹配 "(1:xs)"
fn([2,2,3])->[2,2,3,[]]         //匹配 "(a:b:c:xs)"
fn([2,2,3,4])->[2,2,3,[4]]      //匹配 "(a:b:c:xs)" 
fn([2,2])->[2,[2]]              //匹配 "(x:xs)"
fn({a:1,b:'b'})->1              //匹配 "{a,b:'b'}"
fn({a:2,b:2})->[2,2]
fn({a:{aa:'aa'},b:'b'})->'aa'
fn(100)->100

?函数(?function)

支持自定义函数来匹配 函数返回true表示匹配成功 否则匹配失败 array,object,date,number,string这些函数内置的 用来检查数据类型 自定义函数可传入defunWithFn 第二个参数在当前匹配范围内有效 也可通过 =defun.globalScope =使全局有效

也可直接使用全局函数

//全局有效
defun.globalScope.testfn=function(a){return a=="test"}
var fn = defun({
    "x?array":true,
    "x?object":false,
    "x?testfn":'test'
});
fn([])->true
fn({a:1})->false
fn("test")->'test'
//当前fn有效
var fn = defun({
    "x?array":true,
    "x?object":false,
    "x?testfn":'test'
}{
  "testfn":function(a){return a=="test"}
});