🚀 Launch Week Day 4:Introducing the Alert Details Page: A Better Way to Explore Alerts.Learn More
Socket
Book a DemoInstallSign in
Socket

json-hook-trigger

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-hook-trigger

一個分析 json 內容符不符合條件,符合的話就呼叫對應 function 的 hook

Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

json-hook

一個分析 json 內容符不符合條件,符合的話就呼叫對應 function 的 hook

if source json match aims_object , cell hook function

安裝 install

對! 我原本想要叫 json-hook, 結果有人先搶一步了 Orz...

I originally wanted to call json-hook, but somebody got the first step. Orz...

npm install

npm i -g json-hook-trigger

google apps script library install

打開 gs 編輯頁面
-> "資源"
-> "程式庫"
-> 將 1lfFZa5p7bjHeYIQ0iFwKaE5HnS3ypK2vRVuZaonnXTQLzc0dZBcgeepO 貼上輸入框
-> "新增"
-> 選擇最後版本(記得阿 不然儲存不了)
-> "儲存"~

Open Script Editor.
-> Resource
-> Library
-> Paste Script ID 1lfFZa5p7bjHeYIQ0iFwKaE5HnS3ypK2vRVuZaonnXTQLzc0dZBcgeepO to box
-> Add library
-> select lastest version and save

使用 use

import in NodeJs

const json_hook = require('json-hook-trigger');
var hook = new json_hook.json_hook()

import in TypeScript

import {json_hook} from 'json-hook-trigger'
var hook = new json_hook()

import in Google Apps Script

var hook = new jsonhook.json_hook()
// 'jsonhook' is follow "Identifier"
var source = {
  "update_id": 910469164,
  "message": {
    "message_id": 64609,
    "from": {
      "id": 207014603,
      "is_bot": false,
      "first_name": "永格天",
      "last_name": "(則天)",
      "username": "we684123",
      "language_code": "zh-hant"
    },
    "chat": {
      "id": 207014603,
      "first_name": "永格天",
      "last_name": "(則天)",
      "username": "we684123",
      "type": "private"
    },
    "date": 1594795274,
    "text": "ping"
  }
}
var aims = {
  "and": [{
    'targer': ["message", 'text'],
    'value': 'ping',
    'only_exist': false,
    'use_re': true
  }]
}

function ping(incoming) {
  console.log("get ping time = " + incoming.message.date);
}
hook.addHook(aims, ping)
var incoming = source
hook.macth_run(source,incoming,false) // get ping time = 1594795274

說明 Description

函式說明 function description

hook.addHook(hook_aims, hook_function)

用來綁定 '觸發條件' 與 '要被執行的 function'

ParameterstypeRequiredDescription
hook_aimsaims_objectYes描述比對方式的json,其格式看 aims_object
hook_functionfunction or async functionYes如果比對成功會執行這個 function
Returnvoid

hook.list()

列出當前綁定的 '觸發條件' 跟 '綁定的function'

Returnvoid

hook.macth_run(source, incoming, strict_equality)

用來綁定 '觸發條件' 與 '要被執行的 function'

ParameterstypeRequiredDescription
sourceobjectYes被 hook_aims 比較的 object
incominganyNo要被丟進綁定函數的東西
strict_equalitybooleanNo預設 false ,
如果 false 則執行相等於比較(==),
true 則進行全等於比較(===)
Returnvoid

aims_object 介紹

aims_object 只是一個特定格式的 object。 其內包含 and、or、not 三個條件

顧名思義,and 內列的條件皆須遵守,or 則只要有一個遵守就好 not 則是會把結果反過來,所以 aims['not']['and'] 是皆須不符合 aims['not']['or'] 則是任一不符合

參數 parameterRequired說明 description
aimsYes比對模板json
aims['and']如果沒有 aims['or'],則 Yes在這個array下的條件 皆須符合 才可以
aims['or']如果沒有 aims['and'],則 Yes在這個array下的條件者要 一項符合 就可以
aims['not']No在這個模式下的的 and 跟 or 會 反轉最終結果
aims['not']['and']No在這個array下的條件 皆須不符合 才可以
aims['not']['or']No在這個array下的條件者要 一項不符合 就可以

範例 example

amis = {
  // source['message']['forward_from'] 必須存在
  "and": [{
    'targer': ["message", "forward_from"],
    'value': '',
    'only_exist': true,
    'use_re': false
  }],
  // 當 source['message']['chat']['id'] 為 207014603 或 -1001097080770 都可以接受
  "or": [{
    'targer': ["message", "chat", "id"],
    'value': '207014603',
    'only_exist': false,
    'use_re': false
  }, {
    'targer': ["message", "chat", "id"],
    'value': '-1001097080770',
    'only_exist': false,
    'use_re': false
  }],
  // 不接受當 source['message']['caption'] 等於 '不處理k' 的時候
  "not": {
    "and": [{
      'targer': ["message", "caption"],
      'value': '不處理k',
      'only_exist': false,
      'use_re': false
    }],
    "or": []
  }
}

比對條件格式 condition format

{
  'targer': ["message", "caption"],
  'value': '不處理',
  'only_exist': false,
  'use_re': false
}
參數 parametertypeRequired說明 description
targerString ArrayYes字串陣列,用來尋找指定目標
valueanyYes比對指定目標的值
only_existbooleanYes是否只要指定目標存在就好(!= undefined)
use_rebooleanYes是否啟用 Regex 比對模式

Keywords

json

FAQs

Package last updated on 07 Oct 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts