npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@opi_pib/ts-utility

v2.0.1

Published

Typescript utility

Downloads

28

Readme

@opi_pib/ts-utility

Install

npm install @opi_pib/ts-utility

Assert

Usage

function add(x, y) {
	always(typeof x === "number", "Argument 'x' has to be a number.");
	always(typeof y === "number", "Argument 'y' has to be a number.");
	return x + y;
}

always() function throw an Error if the condition is false:

always(1 > 0); // ok
always(1 < 0); // throws Error

never() does the same but in reverse:

never(1 > 0); // throws Error
never(1 < 0); // ok

TypeScript

function always() are typed to assert that the condition you pass them are true, which gives you certainty that your variable is of a given type at runtime.

export declare function always(condition: boolean, code: string): asserts condition;
const x: unknown = someUntypedFunction();
always(typeof x === "string");
const y = x.toUpperCase(); // TypeScript knows that x must be a string, your IDE can suggest toUpperCase() method

Logic

Is

Static methods of this class will check type of provided value

Is.defined

Is.defined("123"); // true
Is.defined(null); // false
Is.defined(undefined); // false

Is.function

Checks if value is function

Is.function(() => {}); // true
Is.function(""); // false
Is.function(new Promise(() => "")); // false

Is.instanceOf

Checks if value is instance of value

class TestClass {}
Is.InstanceOf(TestClass, new TestClass()); // true
Is.InstanceOf(TestClass, 1); // false

Is.object

Checks if value is object

Is.object({}); // true
Is.object([]); // false
Is.object(() => {}); // false
Is.object(new Promise(() => "")); // false

Is.promise

Checks if value is promise

Is.promise({}); // false
Is.promise(new Promise(() => "")); // true

Is.url

Checks if value is valid url

Is.url("www.google.com"); // false
Is.url("https://google.com"); // true

Other types

Checks if provided value is of provided type

Is.date(new Date()); // true
Is.boolean(true); // true
Is.string(""); // true
Is.null(null); // true
Is.regexp(/someRegularExpression/i); // true
Is.regexp(new RegExp("someRegularExpression", "i")); // true
Is.array([]); // true
Is.number(0); // true
Is.number(NaN); // true
Is.number(Infinity); // true

Models

Entity

Entities are objects that we determine their equality through their identifier.

Example:

interface IdProps {
	id: string;
}

class Id extends ValueObject<IdProps> {
	private constructor(protected readonly props: IdProps) {
		super(props);
	}

	static create(id: string): Id {
		return new Id({ id });
	}
}

interface TestedEntityDto {
	id: Id;
	value: string;
}

interface TestedEntityProps {
	id: Id;
	value: string;
}

class TestedEntity extends Entity<TestedEntityProps> {
	private constructor(protected readonly props: TestedEntityProps) {
		super(props);
	}

	static create(testedEntityDto: TestedEntityDto): TestedEntity {
		return new TestedEntity(testedEntityDto);
	}
}

it("should equals by id", () => {
	const id1 = Id.create("123");
	const id2 = Id.create("123");
	const a = TestedEntity.create({ id: id1, value: "example1" });
	const b = TestedEntity.create({ id: id2, value: "example2" });

	expect(a.equals(b)).toBe(true);
});

it("should not equal if id1 not equals id2", () => {
	const id1 = Id.create("123");
	const id2 = Id.create("1234");
	const a = TestedEntity.create({ id: id1, value: "example" });
	const b = TestedEntity.create({ id: id2, value: "example" });

	expect(a.equals(b)).toBe(false);
});

Value object

ValueObjects are objects that we determine their equality through their structrual property.

Example:

interface TestedValueObjectProps {
	name: string;
	city: {
		name: string;
	};
}

class TestedValueObject extends ValueObject<TestedValueObjectProps> {
	private constructor(protected readonly props: TestedValueObjectProps) {
		super(props);
	}

	static create(name: string, cityName: string): TestedValueObject {
		return new TestedValueObject({ name, city: { name: cityName } });
	}
}

it("should equals by structure", () => {
	const a = TestedValueObject.create("name", "cityName");
	const b = TestedValueObject.create("name", "cityName");

	expect(a.equals(b)).toBe(true);
});

it("should not equals if structure is different", () => {
	const a = TestedValueObject.create("name", "cityName1");
	const b = TestedValueObject.create("name", "cityName2");

	expect(a.equals(b)).toBe(false);
});

Types

Maybe

Marks value as nullable, it can be T, undefined, null. It forces null check on every usage of value.

interface Example {
	a: string;
	b?: Maybe<string>;
}

const obj: Example = {
	a: "string",
	b: "maybeString",
};

const { a, b } = obj;

a.length; // Ok
b.length; // Error, b can be undefined

if (b != null) {
	b.length; // Ok
}

Utils

stripHtml

Removes HTML tags from string

stripHtml('<h1>Title</h1> <p>content</p><img src="">'); // 'Title content'