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

@unpourtous/json-logic-resolve

v2.2.0

Published

resolve logic from json

Downloads

4

Readme

说明

用于判断对象中的属性是否符合json中的条件

问题背景

一般来说,产品都是逻辑放在前端写,通过接口获取具体的数据,从而完成相应的功能

但是,当产品的种类变多时就会有许多需要特殊处理的情况,如果全部都在前端写特殊逻辑,这样会大大地降低代码的扩展性

一个好的思路是把特殊的逻辑抽象出来,简化为true/false的问题,具体来说就是给每个产品配置一个条件(放在json中),用前端的数据对象去匹配,从而达到逻辑配置化的目的

API

resolveLogic(requirement, object, defaultValue)

返回true/false

  • requirement 条件
  • object 需要判断的对象
  • defaultValue默认返回值(不包含条件对应的key或者格式错误)

json的格式说明

原理是基于n叉树的遍历来完成复杂条件的逻辑运算,所以json的结构也是一个n叉树

n叉树上的节点的类型分为两种,统一用operator表示

  • 逻辑节点(&&, ||) 对子节点做逻辑运算
  • 条件节点(match,in,>,>=,<,<=,==) 包含具体的条件
条件类型

目前支持的条件判断类型(requireType)有9种

  • 正则 match
  • 枚举 in
  • 大于 >
  • 大于等于 >=
  • 小于 <
  • 小于等于 <=
  • 等于 ==

配置说明与示例

例如有下面这么一个对象

var date = {
    text: "fdafdffdafdffdafdffdafds",
    year: 10,
    month: 45,
    day: 3,
    hour: 6
};

我们想知道里面的一些值是否满足以下条件

  • month为1或12 (a)
  • month > 10 (b)
  • month < 40 (c)
  • test在1~30个字符之间 (d)
  • day = 3 (e)

然后对这几个条件做如下的的逻辑运算

((a && b && c) && (d || e))

首先转换成前缀表达式

&& && a b c || d e

或者写得更清晰一下

&& (&& a b c) (|| d e)

按照n叉树的结构我们可以修改成

 nodeTree = {
    type: "||",
    nodes: [
        {
            type: "&&",
            nodes: [
                a,
                b,
                c
            ]
        },
        {
            type: "&&",
            nodes: [
                d,
                e
            ]
        }
    ]
}

然后按照函数的规则配置即可

var requirement = {
    operator: "&&",
    operands: [
        {
            operator: "&&",
            operands: [
                {
                    operator: "in",
                    key: "month",
                    range: [1, 12]
                },
                {
                    operator: ">",
                    key: "month",
                    value: 10
                },
                {
                    operator: "<",
                    key: "month",
                    value: 40
                }
            ]
        },
        {
            operator: "||",
            operands: [
                {
                    operator: "match",
                    regExp: "^\\S{1,30}$",
                    key: "text"
                },
                {
                    operator: "==",
                    key: "day",
                    value: 3
                }
            ]
        }
    ]
};

// 结果应该为true
var result  = resolveLogic(requirement, date, false);

更多示例查看/test/index.js