Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@housinganywhere/match

Package Overview
Dependencies
Maintainers
4
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@housinganywhere/match - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

7

dist/cmjs/index.js

@@ -7,1 +7,8 @@ "use strict";

exports.default = match;
exports.wildMatch = function (m) { return function (t) {
var f = m[t];
if (f) {
return f(t);
}
return m._(t);
}; };

@@ -5,1 +5,8 @@ var match = function (m) { return function (t) {

export default match;
export var wildMatch = function (m) { return function (t) {
var f = m[t];
if (f) {
return f(t);
}
return m._(t);
}; };

@@ -16,2 +16,9 @@ (function (factory) {

exports.default = match;
exports.wildMatch = function (m) { return function (t) {
var f = m[t];
if (f) {
return f(t);
}
return m._(t);
}; };
});

@@ -6,1 +6,7 @@ declare type Matcher<T extends string, R> = {

export default match;
declare type PartialMatcher<T extends string, R> = {
[K in T]?: (k: K) => R;
} & {
_: (t: T) => R;
};
export declare const wildMatch: <T extends string, R = void>(m: PartialMatcher<T, R>) => (t: T) => R;

2

package.json
{
"name": "@housinganywhere/match",
"description": "Poor man's pattern matching",
"version": "1.0.0",
"version": "1.1.0",
"main": "./dist/cmjs/index.js",

@@ -6,0 +6,0 @@ "module": "./dist/cmjs/index.js",

@@ -38,5 +38,26 @@ # poor man's pattern matching :traffic_light:

For matching several cases together use **wildMatch**. All the missing cases
will be handled by case `_`.
```ts
import * as React from 'react';
import { wildMatch } from '@housinganywhere/match';
type Vowels = 'a' | 'e' | 'i' | 'o' | 'u';
const isA = wildMatch<Vowels, string>({
a: () => 'Yay!',
_: (v) => `Nope, "${v}" is not "a"`,
});
isA('a'); // 'Yay!'
isA('e'); // 'Nope, "e" is not "a"'
isA('i'); // 'Nope, "i" is not "a"'
isA('o'); // 'Nope, "o" is not "a"'
isA('u'); // 'Nope, "u" is not "a"'
```
## License
[MIT](https://github.com/housinganywhere/match/blob/master/LICENSE) © 2019
HousingAnywhere
[HousingAnywhere](https://housinganywhere.com/)

@@ -1,47 +0,69 @@

import match from './index';
import match, { wildMatch } from './index';
describe('@housinganywhere/match', () => {
it('works with string unions', () => {
type Status = 'foo' | 'bar';
describe('match', () => {
it('works with string unions', () => {
type Status = 'foo' | 'bar';
const m = match<Status, string>({
foo: () => 'FOO',
bar: () => 'BAR',
const m = match<Status, string>({
foo: () => 'FOO',
bar: () => 'BAR',
});
expect(m('foo')).toBe('FOO');
expect(m('bar')).toBe('BAR');
});
expect(m('foo')).toBe('FOO');
expect(m('bar')).toBe('BAR');
});
it('works with string enums', () => {
enum Status {
foo = 'foo',
bar = 'bar',
}
it('works with string enums', () => {
enum Status {
foo = 'foo',
bar = 'bar',
}
const m = match<Status, string>({
foo: () => 'FOO',
bar: () => 'BAR',
});
const m = match<Status, string>({
foo: () => 'FOO',
bar: () => 'BAR',
expect(m(Status.foo)).toBe('FOO');
expect(m(Status.bar)).toBe('BAR');
});
expect(m(Status.foo)).toBe('FOO');
expect(m(Status.bar)).toBe('BAR');
});
it('calls the right handler', () => {
type Status = 'foo' | 'bar';
const handleFoo = jest.fn(() => 'FOO');
const handleBar = jest.fn(() => 'BAR');
it('calls the right handler', () => {
type Status = 'foo' | 'bar';
const handleFoo = jest.fn(() => 'FOO');
const handleBar = jest.fn(() => 'BAR');
const m = match<Status, string>({
foo: handleFoo,
bar: handleBar,
});
const m = match<Status, string>({
foo: handleFoo,
bar: handleBar,
m('foo');
m('bar');
expect(handleFoo).toBeCalled();
expect(handleBar).toBeCalled();
});
});
m('foo');
m('bar');
describe('wildMatch', () => {
it('calls the the default handler when a handler is missing', () => {
type Status = 'foo' | 'bar' | 'baz';
const handleFoo = jest.fn(() => 'FOO');
const handleWild = jest.fn((v) => `called with: ${v}`);
expect(handleFoo).toBeCalled();
expect(handleBar).toBeCalled();
const m = wildMatch<Status, string>({
foo: handleFoo,
_: handleWild,
});
expect(m('foo')).toBe('FOO');
expect(m('bar')).toBe('called with: bar');
expect(m('baz')).toBe('called with: baz');
expect(handleFoo).toBeCalled();
expect(handleWild).toBeCalledTimes(2);
});
});
});

@@ -7,1 +7,17 @@ type Matcher<T extends string, R> = { [K in T]: (k: K) => R };

export default match;
type PartialMatcher<T extends string, R> = { [K in T]?: (k: K) => R } & {
_: (t: T) => R;
};
export const wildMatch = <T extends string, R = void>(
m: PartialMatcher<T, R>,
) => (t: T) => {
const f = m[t];
if (f) {
return f(t);
}
return m._(t);
};
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