Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
tiny-types
Advanced tools
TinyTypes is an npm module 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.
To install the module from npm:
npm install --save 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.
To define a single-value TinyType
- extend from TinyTypeOf<T>()
:
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
value
of type T
, which you can use to access the wrapped primitive value. For example:
const firstName = new FirstName('Jan');
firstName.value === 'Jan';
It also has an equals
method, which you can use to compare tiny types by value:
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):
const name = new FirstName('Jan');
name.toString() === 'FirstName(value=Jan)';
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:
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:
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:
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)))'
Do you find TinyTypes useful? Give it a star! ★
Found a bug? Need a feature? Raise an issue or submit a pull request.
Have feedback? Let me know on twitter: @JanMolak
TinyTypes library is licensed under the Apache-2.0 license.
- Copyright © 2018- Jan Molak
FAQs
A tiny library that brings Tiny Types to JavaScript and TypeScript
The npm package tiny-types receives a total of 9,458 weekly downloads. As such, tiny-types popularity was classified as popular.
We found that tiny-types demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.