{"_id":"@edcarroll/class-discriminators","_rev":"4-e6f52d42c671fdb294f436f5a3365e7c","name":"@edcarroll/class-discriminators","description":"Aids Typescript type checking for class based discriminated unions","dist-tags":{"next":"2.0.0-beta.1","latest":"2.0.0"},"versions":{"1.0.0-beta.1":{"name":"@edcarroll/class-discriminators","version":"1.0.0-beta.1","description":"Aids Typescript type checking for class based discriminated unions","main":"dist/cjs/index.js","types":"dist/es6/index.d.ts","module":"dist/es6/index.js","dependencies":{},"devDependencies":{"@edcarroll/code-style":"^2.0.0","typescript":"^2.3.0"},"scripts":{"lint":"tslint -c tslint.json --type-check --project ./tsconfig.json","compile":"tsc && tsc -p tsconfig.cjs.json","compile:w":"tsc -w","prepublishOnly":"npm run build","build":"npm run lint && npm run compile"},"repository":{"type":"git","url":"git+https://github.com/edcarroll/class-discriminators.git"},"author":{"name":"Edward Carroll"},"license":"MIT","bugs":{"url":"https://github.com/edcarroll/class-discriminators/issues"},"homepage":"https://github.com/edcarroll/class-discriminators#readme","gitHead":"0aaa55bbf30d816050a2df49a51b5f5f04a3d18a","_id":"@edcarroll/class-discriminators@1.0.0-beta.1","_npmVersion":"5.0.4","_nodeVersion":"8.1.3","_npmUser":{"name":"edcarroll","email":"ed@edcarroll.co.uk"},"dist":{"integrity":"sha512-pzVaDEMZJsb0urJ6qcJ5XeRrMhVWJ6Ue8r4nCRSqe9LZ8mJAtZXXcqIOcNI7SC0Zp+CPPGeFI/dXzxaBQ29IsQ==","shasum":"9aaebd6e9aa8f3c1ed71ed45cba4b5a13a421f28","tarball":"https://registry.npmjs.org/@edcarroll/class-discriminators/-/class-discriminators-1.0.0-beta.1.tgz","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDj0rOIk05zAFo9w6SOxqfeh6PoTOUsLJg/RIvE4EccGAIhAP3YCo2Ad+74jlwFS+SLO4z1NQOBhV5WaOswhuLHvxVA"}]},"maintainers":[{"name":"edcarroll","email":"ed@edcarroll.co.uk"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/class-discriminators-1.0.0-beta.1.tgz_1500932925387_0.955030637094751"}},"1.0.0":{"name":"@edcarroll/class-discriminators","version":"1.0.0","description":"Aids Typescript type checking for class based discriminated unions","main":"dist/cjs/index.js","types":"dist/es6/index.d.ts","module":"dist/es6/index.js","dependencies":{},"devDependencies":{"@edcarroll/code-style":"^2.0.0","typescript":"^2.3.0"},"scripts":{"lint":"tslint -c tslint.json --type-check --project ./tsconfig.json","compile":"tsc && tsc -p tsconfig.cjs.json","compile:w":"tsc -w","prepublishOnly":"npm run build","build":"npm run lint && npm run compile"},"repository":{"type":"git","url":"git+https://github.com/edcarroll/class-discriminators.git"},"author":{"name":"Edward Carroll"},"license":"MIT","bugs":{"url":"https://github.com/edcarroll/class-discriminators/issues"},"homepage":"https://github.com/edcarroll/class-discriminators#readme","gitHead":"ad91d989314e7d69470d78ff24642a815c5a0158","_id":"@edcarroll/class-discriminators@1.0.0","_npmVersion":"5.0.4","_nodeVersion":"8.1.3","_npmUser":{"name":"edcarroll","email":"ed@edcarroll.co.uk"},"dist":{"integrity":"sha512-XFd02AmhyJKumCzu9lLRCbMlKunYz5CPAQSyoUUloNoxmG+XtutJUE7O+iJF/zcxA9mSnYWB+bbinimmy7awuw==","shasum":"334c2ef253413d0ae270bb4621f2e5fdf70e7137","tarball":"https://registry.npmjs.org/@edcarroll/class-discriminators/-/class-discriminators-1.0.0.tgz","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDScer2ykThM/c04CJaOtgJupwd0soM8/y5E+7C7XBbWQIhAK2znDYcJofmyHgGnBLKbOU7cU32ulTmp25tZ1iErytF"}]},"maintainers":[{"name":"edcarroll","email":"ed@edcarroll.co.uk"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/class-discriminators-1.0.0.tgz_1500936400278_0.5680195975583047"}},"2.0.0-beta.1":{"name":"@edcarroll/class-discriminators","version":"2.0.0-beta.1","description":"Aids Typescript type checking for class based discriminated unions","main":"dist/cjs/index.js","types":"dist/es6/index.d.ts","module":"dist/es6/index.js","dependencies":{},"devDependencies":{"@edcarroll/code-style":"^2.0.0","typescript":"^2.3.0"},"scripts":{"lint":"tslint -c tslint.json --type-check --project ./tsconfig.json","compile":"tsc && tsc -p tsconfig.cjs.json","compile:w":"tsc -w","prepublishOnly":"npm run build","build":"npm run lint && npm run compile"},"repository":{"type":"git","url":"git+https://github.com/edcarroll/class-discriminators.git"},"author":{"name":"Edward Carroll"},"license":"MIT","bugs":{"url":"https://github.com/edcarroll/class-discriminators/issues"},"homepage":"https://github.com/edcarroll/class-discriminators#readme","gitHead":"968f70f7e3d2602c551a4231977e394c0290fd71","_id":"@edcarroll/class-discriminators@2.0.0-beta.1","_npmVersion":"5.0.4","_nodeVersion":"8.1.3","_npmUser":{"name":"edcarroll","email":"ed@edcarroll.co.uk"},"dist":{"integrity":"sha512-ufWfDxBdWkSioRGEFlCcyY9an9IEBeq9jfUrCFVwd1f4pC6LUjsyTGZlvVLKluRVeDh5sOJGzlx9tzQfVlkv9w==","shasum":"087115fa0d4a0af80f312186f290ef2cf649f661","tarball":"https://registry.npmjs.org/@edcarroll/class-discriminators/-/class-discriminators-2.0.0-beta.1.tgz","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCyh2XL0KGga4MCC34L/3zum03sfLdcDeJTjTyl9/L9FQIhAOqL4znXR3k/CC1hQqXezZZLbPbndK0bxaw+VF4n4m9u"}]},"maintainers":[{"name":"edcarroll","email":"ed@edcarroll.co.uk"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/class-discriminators-2.0.0-beta.1.tgz_1501084401935_0.5214010141789913"}},"2.0.0":{"name":"@edcarroll/class-discriminators","version":"2.0.0","description":"Aids Typescript type checking for class based discriminated unions","main":"dist/cjs/index.js","types":"dist/es6/index.d.ts","module":"dist/es6/index.js","dependencies":{},"devDependencies":{"@edcarroll/code-style":"^2.0.0","typescript":"^2.3.0"},"scripts":{"lint":"tslint -c tslint.json --type-check --project ./tsconfig.json","compile":"tsc && tsc -p tsconfig.cjs.json","compile:w":"tsc -w","prepublishOnly":"npm run build","build":"npm run lint && npm run compile"},"repository":{"type":"git","url":"git+https://github.com/edcarroll/class-discriminators.git"},"author":{"name":"Edward Carroll"},"license":"MIT","bugs":{"url":"https://github.com/edcarroll/class-discriminators/issues"},"homepage":"https://github.com/edcarroll/class-discriminators#readme","gitHead":"78383973dcd052f474427b6abba1d927de7be3be","_id":"@edcarroll/class-discriminators@2.0.0","_npmVersion":"5.0.4","_nodeVersion":"8.1.3","_npmUser":{"name":"edcarroll","email":"ed@edcarroll.co.uk"},"dist":{"integrity":"sha512-c7RbZ79fcZCZOypOmqLBTaEM3m8gAsTMaU6HaHDkstvfX6caddcwkEPnybrdNDkqdvgQ0NwIQyd1h283IeDKIg==","shasum":"b975d60fdd6f86059bd347872ebb08598e095106","tarball":"https://registry.npmjs.org/@edcarroll/class-discriminators/-/class-discriminators-2.0.0.tgz","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIFP+L+EqSdPP6HYZQxpKHg6giYLuuPwF1sn8fk5kehSDAiB/Z73Jmx5qzmOJrl52RTfWFZywNJYO+xtUm9f845Awkw=="}]},"maintainers":[{"name":"edcarroll","email":"ed@edcarroll.co.uk"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/class-discriminators-2.0.0.tgz_1501084857589_0.8586330392863601"}}},"readme":"# Class Discriminators\r\n\r\nAids Typescript type checking for class based discriminated unions.\r\n\r\nNote that the object being tested doesn't need to be an instance of any of the classes, it only needs a discriminator value!\r\n\r\n## Installation\r\n\r\n```sh\r\n$ npm install --save @edcarroll/class-discriminators\r\n```\r\n\r\n## Quickstart\r\n\r\nStart by decorating your classes with their discriminator values:\r\n\r\n```typescript\r\nimport { DiscriminatorValue } from \"@edcarroll/class-discriminators\";\r\n\r\nexport class Hello {\r\n    @DiscriminatorValue(\"hello\")\r\n    public readonly type = \"hello\";\r\n\r\n    public sharedProp = 0;\r\n    public helloProp = 10;\r\n}\r\n\r\nexport class World {\r\n    @DiscriminatorValue(\"world\")\r\n    public readonly type = \"world\";\r\n\r\n    public sharedProp = 1;\r\n    public worldProp = 11;\r\n}\r\n\r\n// HelloWorld is the type union we are going to be narrowing.\r\nexport type HelloWorld = Hello | World;\r\n```\r\n\r\nNext, configure a guard factory using `configureGuardFactory` by providing the type we are narrowing from:\r\n\r\n```typescript\r\nimport { configureGuardFactory } from \"@edcarroll/class-discriminators\";\r\n\r\nconst guardFactory = configureGuardFactory<HelloWorld>();\r\n```\r\n\r\nNote that you can optionally provide a property name to use as the discriminator property.\r\n\r\nNow you can use the guard factory to test the type of various values.\r\n\r\n### Array Filtering\r\n\r\nThe usefulness of this library shows itself when filtering arrays. This library works with the type checker to ensure that when you run `Array.filter` it returns an array typed as the union of the provided types, so that you can then run functions.\r\n\r\nAn example use case would be with Redux / ngrx and class based actions.\r\n\r\n```typescript\r\nconst itemA = new Hello();\r\nconst itemB = new World();\r\n\r\n// Take an array of multiple types\r\nconst arr = [itemA, itemB, itemA, itemB];\r\n\r\n// You can use our configured guard factory to filter down to one or more specified types:\r\nconst hellos = arr\r\n    .filter(guardFactory(Hello))\r\n    .forEach(h => console.log(h.helloProp)); // prints 10, 10.\r\n\r\nconst worlds = console.log(arr\r\n    .filter(guardFactory(World))\r\n    .reduce((s, w) => w.worldProp + s, 0)); // prints 22.\r\n\r\n// As mentioned before, `guardFactory` is a variadic higher order function,\r\n// so you aren't limited to just 1 type:\r\nconst helloWorlds = arr\r\n    .filter(guardFactory(Hello, World))\r\n    .forEach(hw => console.log(hw.sharedProp)); // prints 0, 1, 0, 1.\r\n\r\n// Also works well with Observables:\r\nObservable\r\n    .of(itemA, itemB, itemA, itemB)\r\n    .filter(guardFactory(Hello))\r\n    .subscribe(i => console.log(i.helloProp)); // prints 10, 10\r\n```\r\n\r\n### Individual Guarding\r\n\r\nIndividual guarding is less useful as you can just `switch` on the discriminator property, however it works all the same:\r\n\r\n```typescript\r\n// i.e. we have an item but know only it's a hello or a world.\r\nconst item:HelloWorld = new Hello();\r\n\r\nif (guardFactory(Hello)(item)) {\r\n    console.log(item.helloProp); // prints 10.\r\n} else if (guardFactory(World)(item)) {\r\n    console.log(item.worldProp); // prints 11.\r\n}\r\n\r\n// The above will print 10 as item is a `Hello` (not a surprise for us,\r\n// but at least the type checker is happy!)\r\n```\r\n\r\n### API\r\n\r\n#### `@DiscriminatorValue(value:string):PropertyDecorator`\r\n\r\nProperty decorator that configures a class with the provided discriminator value. Decorator must be applied to the discriminator property as in the examples above.\r\n\r\n#### `configureGuardFactory<T>(discriminatorProperty?:keyof T):GuardFactory<T, ...U extends T>`\r\n\r\nTakes an optional property name override (ommitting this value uses the property the `@DiscriminatorValue` decorator was applied to) to use for discriminator value checks, and returns a `GuardFactory` function.\r\n\r\n##### `GuardFactory<T, ...U extends T>(...types:Class<U>[]):Guard<T, U>`\r\n\r\nTakes `n` classes decorated with `@DiscriminatorValue` and returns a `Guard`.\r\n\r\n*Note that the ...U above is not a real variadic kind, it's emulated using* ***a lot*** *of (up to 15) overflow declarations.*\r\n\r\n##### `Guard<T, U>(guardee:T) => guardee is U`\r\n\r\nTakes an object of type `T` and returns `true` if its discriminator value is equal to one of the ones passed into the guard factory.\r\n\r\n#### `extractDiscriminators<T>(types:Class<T>[]):string[]`\r\n\r\nTakes an array of types and returns an array of their discriminator values.\r\n","maintainers":[{"name":"edcarroll","email":"ed@edcarroll.co.uk"}],"time":{"modified":"2022-04-05T06:40:50.881Z","created":"2017-07-24T21:48:46.338Z","1.0.0-beta.1":"2017-07-24T21:48:46.338Z","1.0.0":"2017-07-24T22:46:41.246Z","2.0.0-beta.1":"2017-07-26T15:53:22.955Z","2.0.0":"2017-07-26T16:00:58.528Z"},"homepage":"https://github.com/edcarroll/class-discriminators#readme","repository":{"type":"git","url":"git+https://github.com/edcarroll/class-discriminators.git"},"author":{"name":"Edward Carroll"},"bugs":{"url":"https://github.com/edcarroll/class-discriminators/issues"},"license":"MIT","readmeFilename":"README.md"}