Skip to content

Commit

Permalink
feat(predicates): startsWith(prefix) and endsWith(suffix) predicates
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-molak committed Apr 11, 2020
1 parent 1c0c2f3 commit eca2378
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 0 deletions.
36 changes: 36 additions & 0 deletions spec/predicates/endsWith.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'mocha';
import { given } from 'mocha-testdata';

import { endsWith, ensure, TinyType } from '../../src';
import { expect } from '../expect';

describe('predicates', () => {

/** @test {endsWith} */
describe('::endsWith', () => {

class TextFileName extends TinyType {
constructor(public readonly value: string) {
super();

ensure('TextFileName', value, endsWith('.txt'));
}
}

it('ensures that the argument ends with a given suffix', () => {
expect(() => new TextFileName('notes.txt')).to.not.throw; // tslint:disable-line:no-unused-expression
});

given([
undefined,
null,
{},
'string',
5,
]).
it('complains if the value does not end with a given suffix', (value: any) => {
expect(() => new TextFileName(value))
.to.throw(`TextFileName should end with '.txt'`);
});
});
});
36 changes: 36 additions & 0 deletions spec/predicates/startsWith.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'mocha';
import { given } from 'mocha-testdata';

import { ensure, startsWith, TinyType } from '../../src';
import { expect } from '../expect';

describe('predicates', () => {

/** @test {startsWith} */
describe('::startsWith', () => {

class Username extends TinyType {
constructor(public readonly value: string) {
super();

ensure('Username', value, startsWith('usr'));
}
}

it('ensures that the argument starts with a given prefix', () => {
expect(() => new Username('usr123')).to.not.throw; // tslint:disable-line:no-unused-expression
});

given([
undefined,
null,
{},
'string',
5,
]).
it('complains if the value does not start with the given prefix', (value: any) => {
expect(() => new Username(value))
.to.throw(`Username should start with 'usr'`);
});
});
});
24 changes: 24 additions & 0 deletions src/predicates/endsWith.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Predicate } from './Predicate';

/**
* @desc Ensures that the `value` ends with a given suffix.
*
* @example
* import { endsWith, ensure, TinyType } from 'tiny-types';
*
* class TextFileName extends TinyType {
* constructor(public readonly value: string) {
* super();
*
* ensure('TextFileName', value, endsWith('.txt'));
* }
* }
*
* @returns {Predicate<string>}
*/
export function endsWith(suffix: string): Predicate<string> {
return Predicate.to(`end with '${ suffix }'`, (value: string) =>
typeof value === 'string'
&& value.endsWith(suffix),
);
}
2 changes: 2 additions & 0 deletions src/predicates/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './and';
export * from './endsWith';
export * from './hasLengthOf';
export * from './isArray';
export * from './isBoolean';
Expand All @@ -18,3 +19,4 @@ export * from './matches';
export * from './or';
export * from './Predicate';
export * from './property';
export * from './startsWith';
24 changes: 24 additions & 0 deletions src/predicates/startsWith.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Predicate } from './Predicate';

/**
* @desc Ensures that the `value` starts with a given prefix.
*
* @example
* import { ensure, startsWith, TinyType } from 'tiny-types';
*
* class Username extends TinyType {
* constructor(public readonly value: string) {
* super();
*
* ensure('Username', value, startsWith('usr'));
* }
* }
*
* @returns {Predicate<string>}
*/
export function startsWith(prefix: string): Predicate<string> {
return Predicate.to(`start with '${ prefix }'`, (value: string) =>
typeof value === 'string'
&& value.startsWith(prefix),
);
}

0 comments on commit eca2378

Please sign in to comment.