New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

data-matching

Package Overview
Dependencies
Maintainers
1
Versions
57
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

data-matching

Matches a data object against a reference value Edit

  • 1.39.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

data-matching

Matches a data object against a match object.

Syntax details

  • Part of strings can be collected using '!{name}' syntax (see https://github.com/MayamaTakeshi/string-matching)
  • data can be collected (stored in a dictionary passed to matching function) by using dm.collect('name')
  • data that should be absent can be declared with dm.absent

Sample usage

Full match (all specified data must be present):

const dm = require("data-matching")
const assert = require('assert')

var received = {
	connection: { ip: '192.168.2.10' },
	media: [
		{
			type: 'application',
			port: 9,
			protocol: 'TCP/MRCPv2',
			payloads: ["0"],
			setup: 'active',
			connection: 'new',
			resource: 'speechsynth'
		},
		{
			type: 'audio',
			port: 14238,
			protocol: 'RTP/AVP',
			payloads: ["0", "8", "96"],
		}
	]
}

var expected = {
	connection: { ip: dm.collect('remote_ip') },
	media: [
		{
			type: 'application',
			port: 9,
			protocol: '!{transport_protocol}/MRCP!{mrcp_version}',
			payloads: [dm.collect('mrcp_payload')],
			setup: 'active',
			connection: 'new',
			resource: dm.any_of(['speechsynth', 'speechrecog'], 'media_resource')
		},
		{
			type: 'audio',
			port: 14238,
			protocol: 'RTP/AVP',
			payloads: ["0", "8", "96"],
		}
	]
}

var store = {}
assert(dm.full_match(expected)(received, store))
assert(store.remote_ip == "192.168.2.10")
assert(store.mrcp_payload == "0")
assert(store.media_resource == 'speechsynth')
assert(store.mrcp_version == 'v2')
assert(store.transport_protocol == 'TCP')

Partial match (only part of the data must match):

const dm = require("data-matching")
const assert = require('assert')


var received = {
	request_uri: 'sip:alice@abc.com',
	from_uri: '<sip:bob@cba.com>',
	to_uri: 'sip:alice@abc.com',
	call_id: 'ee98b779-3048-41cf-b85b-0a05bcc4038a',
	cseq: 'INVITE 50',
}


var expected = {
	request_uri: 'sip:!{user1}@!{domain1}',
	from_uri: '<sip:!{user2}@!{domain2}>',
	cseq: '!{method} 50',
	some_absent_key: dm.absent,
}


var store = {}
assert(dm.partial_match(expected)(received, store))
assert(store.user1 == 'alice')
assert(store.domain1 == 'abc.com')
assert(store.user2 == 'bob')
assert(store.domain2 == 'cba.com')
assert(store.method == 'INVITE')

More examples:

See https://github.com/MayamaTakeshi/data-matching/blob/master/test/matching.test.js

Keywords

FAQs

Package last updated on 29 Feb 2024

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc