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

tiny-types

Package Overview
Dependencies
Maintainers
1
Versions
47
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tiny-types - npm Package Compare versions

Comparing version 1.0.1 to 1.1.0

lib/match.d.ts

2

lib/index.d.ts

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

export * from './match';
export * from './TinyType';
export * from './pattern-matching/PatternMatcher';

@@ -6,3 +6,5 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
__export(require("./match"));
__export(require("./TinyType"));
__export(require("./pattern-matching/PatternMatcher"));
//# sourceMappingURL=index.js.map

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

export declare function TinyTypeOf<T>(): {
new (_: T): {
value: T;
} & TinyType;
};
export declare abstract class TinyType {

@@ -2,0 +7,0 @@ equals(another: TinyType): any;

"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
function TinyTypeOf() {
return /** @class */ (function (_super) {
__extends(class_1, _super);
function class_1(value) {
var _this = _super.call(this) || this;
_this.value = value;
return _this;
}
return class_1;
}(TinyType));
}
exports.TinyTypeOf = TinyTypeOf;
var TinyType = /** @class */ (function () {

@@ -31,3 +53,4 @@ function TinyType() {

return Object.getOwnPropertyNames(this)
.filter(function (field) { return typeof _this[field] !== 'function'; });
.filter(function (field) { return typeof _this[field] !== 'function'; })
.sort();
};

@@ -34,0 +57,0 @@ return TinyType;

5

package.json
{
"name": "tiny-types",
"version": "1.0.1",
"version": "1.1.0",
"description": "A tiny library that brings Tiny Types to JavaScript and TypeScript",

@@ -70,7 +70,6 @@ "main": "lib/index.js",

"exclude": [
"features",
"lib",
"node_modules",
"spec",
"staging"
"src/types.ts"
],

@@ -77,0 +76,0 @@ "extension": [

@@ -8,2 +8,130 @@ # Tiny Types

[![npm](https://img.shields.io/npm/dm/tiny-types.svg)](https://npm-stat.com/charts.html?package=tiny-types)
[![Known Vulnerabilities](https://snyk.io/test/github/jan-molak/tiny-types/badge.svg)](https://snyk.io/test/github/jan-molak/tiny-types)
[![Known Vulnerabilities](https://snyk.io/test/github/jan-molak/tiny-types/badge.svg)](https://snyk.io/test/github/jan-molak/tiny-types)
TinyTypes is an [npm module](https://www.npmjs.com/package/tiny-types) that makes it easy for TypeScript and JavaScript
projects to give domain meaning to primitive types. It also helps to avoid all sorts of bugs
and makes your code easier to refactor.
## Installation
To install the module from npm:
```
npm install --save tiny-types
```
## Defining Tiny Types
> An int on its own is just a scalar with no meaning. With an object, even a small one, you are giving both the compiler
and the programmer additional information about what the value is and why it is being used.
>
> &dash; [Jeff Bay, Object Calisthenics](http://www.xpteam.com/jeff/writings/objectcalisthenics.rtf)
### Single-value types
To define a single-value `TinyType` - extend from `TinyTypeOf<T>()`:
```typescript
import { TinyTypeOf } from 'tiny-types';
class FirstName extends TinyTypeOf<string>() {}
class LastName extends TinyTypeOf<string>() {}
class Age extends TinyTypeOf<number>() {}
```
Every tiny type defined this way has
a [readonly property](https://www.typescriptlang.org/docs/handbook/classes.html#readonly-modifier)
`value` of type `T`, which you can use to access the wrapped primitive value. For example:
```typescript
const firstName = new FirstName('Jan');
firstName.value === 'Jan';
```
It also has an `equals` method, which you can use to compare tiny types by value:
```typescript
const
name1 = new FirstName('Jan'),
name2 = new FirstName('Jan');
name1.equals(name2) === true;
```
An additional feature of tiny types is a built-in `toString()` method (which you can override if you want to, of course):
```typescript
const name = new FirstName('Jan');
name.toString() === 'FirstName(value=Jan)';
```
### Multi-value and complex types
If the tiny type you want to model has more than one value,
or you want to perform additional operations in the constructor,
extend from `TinyType` directly:
```typescript
import { TinyType } from 'tiny-types';
class Person extends TinyType {
constructor(public readonly firstName: FirstName,
public readonly lastName: LastName,
) {
super();
}
}
```
You can also mix and match both of the above definition styles:
```typescript
import { TinyType, TinyTypeOf } from 'tiny-types';
class UserName extends TinyTypeOf<string>() {}
class Timestamp extends TinyTypeOf<Date>() {}
abstract class DomainEvent extends TinyTypeOf<Timestamp>() {}
class AccountCreated extends DomainEvent {
constructor(public readonly username: UserName, timestamp: Timestamp) {
super(timestamp);
}
}
const event = new AccountCreated(new UserName('jan-molak'), new Timestamp(new Date()));
```
Even such complex types still have both the `equals` and `toString` methods:
```typescript
const
now = new Date(2018, 2, 12, 0, 30),
event1 = new AccountCreated(new UserName('jan-molak'), new Timestamp(now)),
event2 = new AccountCreated(new UserName('jan-molak'), new Timestamp(now));
event1.equals(event2) === true;
event1.toString() === 'AccountCreated(username=UserName(value=jan-molak), value=Timestamp(value=Mon Mar 12 2018 00:30:00 GMT+0000 (GMT)))'
```
## Your feedback matters!
Do you find TinyTypes useful? Give it a star! &#9733;
Found a bug? Need a feature? Raise [an issue](https://github.com/jan-molak/tiny-types/issues?state=open)
or submit a pull request.
Have feedback? Let me know on twitter: [@JanMolak](https://twitter.com/JanMolak)
## License
TinyTypes library is licensed under the [Apache-2.0](LICENSE.md) license.
----
_- Copyright &copy; 2018- [Jan Molak](https://janmolak.com)_

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

export * from './match';
export * from './TinyType';
export * from './pattern-matching/PatternMatcher';

@@ -0,2 +1,11 @@

export function TinyTypeOf<T>(): { new(_: T): { value: T } & TinyType } {
return class extends TinyType {
constructor(public readonly value: T) {
super();
}
};
}
export abstract class TinyType {
equals(another: TinyType) {

@@ -12,2 +21,3 @@ if (another === this) {

return this.fields().reduce((previousFieldsAreEqual: boolean, field: string) => {
const currentFieldIsEqual = (this[field].equals

@@ -31,4 +41,5 @@ ? this[field].equals(another[field])

return Object.getOwnPropertyNames(this)
.filter(field => typeof this[field] !== 'function');
.filter(field => typeof this[field] !== 'function')
.sort();
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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