simplytyped
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -6,2 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./types")); | ||
__export(require("./SchemaValidation")); |
@@ -12,4 +12,4 @@ "use strict"; | ||
function allTrue(x) { | ||
let ret = true; | ||
x.forEach((y) => ret = ret && y); | ||
var ret = true; | ||
x.forEach(function (y) { return ret = ret && y; }); | ||
return ret; | ||
@@ -23,5 +23,5 @@ } | ||
return false; | ||
const props = schema.properties; | ||
const keys = Object.keys(props); | ||
const results = keys.map((k) => checkSchema(data[k], props[k])); | ||
var props_1 = schema.properties; | ||
var keys = Object.keys(props_1); | ||
var results = keys.map(function (k) { return checkSchema(data[k], props_1[k]); }); | ||
return allTrue(results); | ||
@@ -41,3 +41,3 @@ } | ||
return false; | ||
const results = data.map((x) => checkSchema(x, schema.items)); | ||
var results = data.map(function (x) { return checkSchema(x, schema.items); }); | ||
return allTrue(results); | ||
@@ -44,0 +44,0 @@ } |
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./types/functions")); |
@@ -0,2 +1,8 @@ | ||
import { If } from './conditionals'; | ||
import { IsAny } from './predicates'; | ||
import { DeepReadonly } from './objects'; | ||
export declare type ConstructorFunction<T extends object> = new (...args: any[]) => T; | ||
export declare type Predicate = (...args: any[]) => boolean; | ||
export declare type Predicate<A = any> = If<IsAny<A>, (...args: any[]) => boolean, (arg: A) => boolean>; | ||
export declare function Readonly<T extends object>(obj: T): DeepReadonly<T>; | ||
export declare function isKeyOf<T extends object>(obj: T, k: string): k is keyof T; | ||
export declare function objectKeys<T extends object>(obj: T): (keyof T)[]; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function Readonly(obj) { return obj; } | ||
exports.Readonly = Readonly; | ||
function isKeyOf(obj, k) { | ||
return Object.keys(obj).includes(k); | ||
} | ||
exports.isKeyOf = isKeyOf; | ||
function objectKeys(obj) { | ||
return Object.keys(obj); | ||
} | ||
exports.objectKeys = objectKeys; |
{ | ||
"name": "simplytyped", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "yet another Typescript type library for advanced types", | ||
@@ -5,0 +5,0 @@ "main": "dist/src/index", |
@@ -46,3 +46,3 @@ # SimplyTyped | ||
[Predicate](#predicate) - [ConstructorFunction](#constructorfunction) | ||
[Predicate](#predicate) - [ConstructorFunction](#constructorfunction) - [Readonly](#readonly) - [isKeyOf](#isKeyOf) - [objectKeys](#objectkeys) | ||
@@ -371,2 +371,32 @@ **[Schema Validation](#schema-validation)** | ||
### Readonly | ||
This takes a runtime object and makes its properties readonly. | ||
Useful for declare object literals, but using inferred types. | ||
```ts | ||
const config = Readonly({ | ||
url: 'https://example.com', | ||
password: 'immasecurepassword', | ||
}); // => { url: readonly string, password: readonly string } | ||
``` | ||
### isKeyOf | ||
Type guard returning true if `k` is a key in `obj`. | ||
```ts | ||
const obj = { a: '' }; | ||
const k = 'a'; | ||
if (isKeyOf(obj, k)) { | ||
obj[k] = 'hi'; // typeof k === 'a' | ||
} else { | ||
throw new Error('oops'); // typeof k === string | ||
} | ||
``` | ||
### objectKeys | ||
Same as `Object.keys` except that the returned type is an array of keys of the object. | ||
Note that for the same reason that `Object.keys` does not do this natively, this method _is not safe_ for objects on the perimeter of your code (user input, read in files, network requests etc.). | ||
```ts | ||
const obj = { a: '', b: 22 }; | ||
const keys = objectKeys(obj); // Array<'a' | 'b'> | ||
``` | ||
## Schema Validation | ||
@@ -373,0 +403,0 @@ One of the easiest points of failure with the typescript type system is outside data. |
25052
204
430