tiny-types
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -0,1 +1,2 @@ | ||
import { JSONValue } from './types'; | ||
export declare function TinyTypeOf<T>(): { | ||
@@ -9,3 +10,4 @@ new (_: T): { | ||
toString(): string; | ||
toJSON(): JSONValue; | ||
private fields(); | ||
} |
@@ -50,2 +50,24 @@ "use strict"; | ||
}; | ||
TinyType.prototype.toJSON = function () { | ||
var _this = this; | ||
var isPrimitive = function (value) { return Object(value) !== value; }; | ||
function toJSON(value) { | ||
switch (true) { | ||
case value && !!value.toJSON: | ||
return value.toJSON(); | ||
case value && !isPrimitive(value): | ||
return JSON.stringify(value); | ||
default: | ||
return value; | ||
} | ||
} | ||
var fields = this.fields(); | ||
if (fields.length === 1) { | ||
return toJSON(this[fields[0]]); | ||
} | ||
return fields.reduce(function (acc, field) { | ||
acc[field] = toJSON(_this[field]); | ||
return acc; | ||
}, {}); | ||
}; | ||
TinyType.prototype.fields = function () { | ||
@@ -52,0 +74,0 @@ var _this = this; |
@@ -5,1 +5,8 @@ export declare type List<T> = T[]; | ||
}; | ||
export declare type JSONPrimitive = string | number | boolean | null; | ||
export interface JSONObject { | ||
[_: string]: JSONPrimitive | JSONObject | JSONArray; | ||
} | ||
export interface JSONArray extends Array<JSONValue> { | ||
} | ||
export declare type JSONValue = JSONPrimitive | JSONObject | JSONArray; |
{ | ||
"name": "tiny-types", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "A tiny library that brings Tiny Types to JavaScript and TypeScript", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -51,4 +51,6 @@ # Tiny Types | ||
It also has an `equals` method, which you can use to compare tiny types by value: | ||
#### Equals | ||
Each tiny type object has an `equals` method, which you can use to compare it by value: | ||
```typescript | ||
@@ -62,4 +64,6 @@ const | ||
An additional feature of tiny types is a built-in `toString()` method (which you can override if you want to, of course): | ||
#### ToString | ||
An additional feature of tiny types is a built-in `toString()` method: | ||
```typescript | ||
@@ -71,2 +75,16 @@ const name = new FirstName('Jan'); | ||
Which you can override if you want to: | ||
```typescript | ||
class Timestamp extends TinyTypeOf<Date>() { | ||
toString() { | ||
return `Timestamp(value=${this.value.toISOString()})`; | ||
} | ||
} | ||
const timestamp = new Timestamp(new Date()); | ||
timestampt.toString() === 'Timestamp(value=2018-03-12T00:30:00.000Z))' | ||
``` | ||
### Multi-value and complex types | ||
@@ -98,3 +116,7 @@ | ||
class Timestamp extends TinyTypeOf<Date>() {} | ||
class Timestamp extends TinyTypeOf<Date>() { | ||
toString() { | ||
return `Timestamp(value=${this.value.toISOString()})`; | ||
} | ||
} | ||
@@ -122,3 +144,3 @@ abstract class DomainEvent extends TinyTypeOf<Timestamp>() {} | ||
event1.toString() === 'AccountCreated(username=UserName(value=jan-molak), value=Timestamp(value=Mon Mar 12 2018 00:30:00 GMT+0000 (GMT)))' | ||
event1.toString() === 'AccountCreated(username=UserName(value=jan-molak), value=Timestamp(value=2018-03-12T00:30:00.000Z))' | ||
``` | ||
@@ -125,0 +147,0 @@ |
@@ -0,1 +1,3 @@ | ||
import { JSONValue } from './types'; | ||
export function TinyTypeOf<T>(): { new(_: T): { value: T } & TinyType } { | ||
@@ -38,2 +40,27 @@ return class extends TinyType { | ||
toJSON(): JSONValue { | ||
const isPrimitive = (value: any) => Object(value) !== value; | ||
function toJSON(value: any) { | ||
switch (true) { | ||
case value && !! value.toJSON: | ||
return value.toJSON(); | ||
case value && ! isPrimitive(value): | ||
return JSON.stringify(value); | ||
default: | ||
return value; | ||
} | ||
} | ||
const fields = this.fields(); | ||
if (fields.length === 1) { | ||
return toJSON(this[fields[0]]); | ||
} | ||
return fields.reduce((acc, field) => { | ||
acc[field] = toJSON(this[field]); | ||
return acc; | ||
}, {}); | ||
} | ||
private fields() { | ||
@@ -40,0 +67,0 @@ return Object.getOwnPropertyNames(this) |
export type List<T> = T[]; | ||
export type ConstructorOrAbstract<T = {}> = Function & { prototype: T }; // tslint:disable-line:ban-types | ||
export type JSONPrimitive = string | number | boolean | null; | ||
export interface JSONObject { | ||
[_: string]: JSONPrimitive | JSONObject | JSONArray; | ||
} | ||
export interface JSONArray extends Array<JSONValue> {} // tslint:disable-line:no-empty-interface | ||
export type JSONValue = JSONPrimitive | JSONObject | JSONArray; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
60175
739
159
0