🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

io-ts-immutable

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

io-ts-immutable - npm Package Compare versions

Comparing version
0.1.0
to
0.2.0
+10
lib/set.d.ts
import * as t from "io-ts";
import { Set } from "immutable";
/**
* A codec for the Set type from immutable.
* This codec decodes a javascript array into an immutable set, and it encodes the immutable set to a javascript array
* @param codec The codec for the set items.
* @since 0.1.0
*/
export declare function set<C extends t.Mixed>(codec: C): t.Type<Set<C["_A"]>, C["_O"][], unknown>;
//# sourceMappingURL=set.d.ts.map
{"version":3,"file":"set.d.ts","sourceRoot":"","sources":["../src/set.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,OAAO,CAAC;AAC3B,OAAO,EAAC,GAAG,EAAC,MAAM,WAAW,CAAC;AAG9B;;;;;GAKG;AACH,wBAAgB,GAAG,CAAC,CAAC,SAAS,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,CAAC,4CAgB9C"}
"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
var t = __importStar(require("io-ts"));
var immutable_1 = require("immutable");
var Either_1 = require("fp-ts/lib/Either");
/**
* A codec for the Set type from immutable.
* This codec decodes a javascript array into an immutable set, and it encodes the immutable set to a javascript array
* @param codec The codec for the set items.
* @since 0.1.0
*/
function set(codec) {
return new t.Type("Set<" + codec.name + ">", function (u) { return immutable_1.Set.isSet(u) && u.every(function (uItem) { return codec.is(uItem); }); }, function (u, context) {
var decodedArray = t.array(codec).validate(u, context);
if (Either_1.isLeft(decodedArray)) {
return t.failure(u, context, "Could not decode input to an array");
}
return t.success(immutable_1.Set(decodedArray.right));
}, function (s) { return s.map(codec.encode).toArray(); });
}
exports.set = set;
//# sourceMappingURL=set.js.map
{"version":3,"file":"set.js","sourceRoot":"","sources":["../src/set.ts"],"names":[],"mappings":";;;;;;;;;AAAA,uCAA2B;AAC3B,uCAA8B;AAC9B,2CAAwC;AAExC;;;;;GAKG;AACH,SAAgB,GAAG,CAAoB,KAAQ;IAI7C,OAAO,IAAI,CAAC,CAAC,IAAI,CACf,SAAO,KAAK,CAAC,IAAI,MAAG,EACpB,UAAC,CAAU,IAAqB,OAAA,eAAG,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,UAAA,KAAK,IAAI,OAAA,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,EAAf,CAAe,CAAC,EAAjD,CAAiD,EACjF,UAAC,CAAU,EAAE,OAAO;QAChB,IAAM,YAAY,GAAG,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC;QACzD,IAAG,eAAM,CAAC,YAAY,CAAC,EAAE;YACrB,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC,EAAE,OAAO,EAAE,oCAAoC,CAAC,CAAC;SACtE;QACD,OAAO,CAAC,CAAC,OAAO,CAAC,eAAG,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC,CAAC;IAC9C,CAAC,EACD,UAAC,CAAC,IAAK,OAAA,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,OAAO,EAAE,EAA7B,CAA6B,CACrC,CAAC;AACJ,CAAC;AAhBD,kBAgBC"}
import * as t from "io-ts";
import {Set} from "immutable";
import {isLeft} from "fp-ts/lib/Either";
/**
* A codec for the Set type from immutable.
* This codec decodes a javascript array into an immutable set, and it encodes the immutable set to a javascript array
* @param codec The codec for the set items.
* @since 0.1.0
*/
export function set<C extends t.Mixed>(codec: C) {
type Item = t.TypeOf<C>;
type ItemOutput = t.OutputOf<C>;
return new t.Type<Set<Item>, Array<ItemOutput>, unknown>(
`Set<${codec.name}>`,
(u: unknown): u is Set<Item> => Set.isSet(u) && u.every(uItem => codec.is(uItem)),
(u: unknown, context) => {
const decodedArray = t.array(codec).validate(u, context);
if(isLeft(decodedArray)) {
return t.failure(u, context, "Could not decode input to an array");
}
return t.success(Set(decodedArray.right));
},
(s) => s.map(codec.encode).toArray()
);
}
import * as tImmutable from "../lib/index";
import * as t from "io-ts";
import {Set} from "immutable";
import {isRight} from "fp-ts/lib/Either";
describe("is", () => {
it("is returns true if u is a set with all items matching the given codec", () => {
const isSet = tImmutable.set(t.string).is(Set.of("a", "b", "c"));
expect(isSet).toBeTruthy();
});
it("is returns false if u is a set and only some items match the given codec", () => {
const isSet = tImmutable.set(t.string).is(Set.of<any>("a", 1, "b"));
expect(isSet).toBeFalsy();
});
it("is returns false if u is a set and none of the items match the given codec", () =>{
const isSet = tImmutable.set(t.string).is(Set.of(1,2,3));
expect(isSet).toBeFalsy();
});
it("is returns false if u is not a set", () => {
const isSet = tImmutable.set(t.string).is(["a", "b", "c"]);
expect(isSet).toBeFalsy();
});
});
describe("decode", () => {
it("decodes an empty array", () => {
const input: Array<string> = [];
const output = tImmutable.set(t.string).decode(input);
expect(isRight(output)).toBeTruthy();
// @ts-ignore
expect(output.right).toEqual(Set.of())
});
it("decodes a non empty array", () => {
const input = ["a", "b", "c"];
const output = tImmutable.set(t.string).decode(input);
expect(isRight(output)).toBeTruthy();
// @ts-ignore
expect(output.right).toEqual(Set.of("a", "b", "c"));
});
it("fails decoding an array with the wrong content", () => {
const input = ["a", "b", "c"];
const output = tImmutable.set(t.number).decode(input);
expect(isRight(output)).toBeFalsy();
});
it("fails decoding a object that is not an array", () => {
const input = "abc";
const output = tImmutable.set(t.string).decode(input);
expect(isRight(output)).toBeFalsy();
});
});
describe("encode", () => {
it("encodes an empty set", ()=> {
const input = Set();
const output = tImmutable.set(t.string).encode(input);
expect(output).toEqual([]);
});
it("encodes a non empty set", () => {
const input = Set.of("1", "2", "3");
const output = tImmutable.set(t.string).encode(input);
expect(output).toEqual(["1", "2", "3"]);
});
});
+1
-0
export { list } from "./list";
export { map } from "./map";
export { set } from "./set";
//# sourceMappingURL=index.d.ts.map
+1
-1

@@ -1,1 +0,1 @@

{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,IAAI,EAAC,MAAM,QAAQ,CAAC;AAC5B,OAAO,EAAC,GAAG,EAAC,MAAM,OAAO,CAAC"}
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,IAAI,EAAC,MAAM,QAAQ,CAAC;AAC5B,OAAO,EAAC,GAAG,EAAC,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAC,GAAG,EAAC,MAAM,OAAO,CAAC"}

@@ -7,2 +7,4 @@ "use strict";

exports.map = map_1.map;
var set_1 = require("./set");
exports.set = set_1.set;
//# sourceMappingURL=index.js.map

@@ -1,1 +0,1 @@

{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,+BAA4B;AAApB,sBAAA,IAAI,CAAA;AACZ,6BAA0B;AAAlB,oBAAA,GAAG,CAAA"}
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,+BAA4B;AAApB,sBAAA,IAAI,CAAA;AACZ,6BAA0B;AAAlB,oBAAA,GAAG,CAAA;AACX,6BAA0B;AAAlB,oBAAA,GAAG,CAAA"}
{
"name": "io-ts-immutable",
"version": "0.1.0",
"version": "0.2.0",
"description": "A collection of io-ts codecs for the immutable collections from immutable-js",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

export {list} from "./list";
export {map} from "./map";
export {map} from "./map";
export {set} from "./set";