-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(predicates): startsWith(prefix) and endsWith(suffix) predicates
- Loading branch information
Showing
5 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'`); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'`); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
); | ||
} |