@othree.io/cerillo 0.1.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.gitlab-ci.yml CHANGED
@@ -4,11 +4,12 @@ cache:
4
4
  key: ${CI_PIPELINE_ID}
5
5
  paths:
6
6
  - node_modules/
7
+ - lib/
7
8
 
8
9
  build:
9
10
  stage: build
10
11
  script:
11
- - npm install
12
+ - npm install && npm run build
12
13
 
13
14
  test:
14
15
  stage: test
@@ -16,10 +17,13 @@ test:
16
17
  - npm test
17
18
 
18
19
  deploy:
20
+ image: node:14
19
21
  stage: deploy
22
+ variables:
23
+ NPM_CONFIG_USERCONFIG: '$CI_PROJECT_DIR/.npmrc'
20
24
  script:
21
25
  - cat $NPM > .npmrc
22
- - npm publish --access public
26
+ - npx semantic-release
23
27
  only:
24
28
  refs:
25
- - master
29
+ - master
package/lib/match.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ declare type ThenCallback<A, B> = (value: A) => B;
2
+ declare type WhenCallback<A> = (value: A) => boolean;
3
+ export declare class MatchError extends Error {
4
+ constructor(message: string);
5
+ }
6
+ export interface Match<A, B> {
7
+ readonly get: () => B;
8
+ readonly when: (callback: WhenCallback<A>) => {
9
+ readonly then: (callback: ThenCallback<A, B>) => Match<A, B>;
10
+ };
11
+ readonly default: (callback: ThenCallback<A, B>) => Match<A, B>;
12
+ }
13
+ export declare type MatchFn = <A, B>(value: A) => Match<A, B>;
14
+ export declare const match: MatchFn;
15
+ export {};
16
+ //# sourceMappingURL=match.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"match.d.ts","sourceRoot":"","sources":["../src/match.ts"],"names":[],"mappings":"AAAA,aAAK,YAAY,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,CAAA;AACzC,aAAK,YAAY,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,CAAA;AAE5C,qBAAa,UAAW,SAAQ,KAAK;gBACrB,OAAO,EAAE,MAAM;CAU9B;AAED,MAAM,WAAW,KAAK,CAAC,CAAC,EAAE,CAAC;IACvB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC,CAAA;IACrB,QAAQ,CAAC,IAAI,EAAE,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC,CAAC,KAAK;QAC1C,QAAQ,CAAC,IAAI,EAAE,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;KAC/D,CAAA;IACD,QAAQ,CAAC,OAAO,EAAE,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;CAClE;AAED,oBAAY,OAAO,GAAG,CAAC,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,KAAK,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;AAoDrD,eAAO,MAAM,KAAK,EAAE,OAA0C,CAAA"}
package/lib/match.js ADDED
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.match = exports.MatchError = void 0;
4
+ class MatchError extends Error {
5
+ constructor(message) {
6
+ super(message);
7
+ /* istanbul ignore next */
8
+ if (Error.captureStackTrace) {
9
+ Error.captureStackTrace(this, MatchError);
10
+ }
11
+ this.name = 'MatchError';
12
+ }
13
+ }
14
+ exports.MatchError = MatchError;
15
+ function unboundMatch(value) {
16
+ const patterns = [];
17
+ let def = undefined;
18
+ const when = function (validationCallback) {
19
+ const then = function (thenCallback) {
20
+ patterns.push({
21
+ when: validationCallback,
22
+ then: thenCallback
23
+ });
24
+ return this;
25
+ }.bind(this);
26
+ return {
27
+ then
28
+ };
29
+ };
30
+ const defaultMatch = function (callback) {
31
+ def = callback;
32
+ return this;
33
+ };
34
+ const response = {
35
+ get: () => {
36
+ const pattern = patterns.find(pattern => pattern.when(value));
37
+ if (pattern) {
38
+ return pattern.then(value);
39
+ }
40
+ else {
41
+ if (def) {
42
+ return def(value);
43
+ }
44
+ else {
45
+ throw new MatchError(`Nothing matched for value: ${value}`);
46
+ }
47
+ }
48
+ },
49
+ when: undefined,
50
+ default: undefined
51
+ };
52
+ response.when = when.bind(response);
53
+ response.default = defaultMatch.bind(response);
54
+ return Object.freeze(response);
55
+ }
56
+ exports.match = unboundMatch.bind(unboundMatch);
57
+ //# sourceMappingURL=match.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"match.js","sourceRoot":"","sources":["../src/match.ts"],"names":[],"mappings":";;;AAGA,MAAa,UAAW,SAAQ,KAAK;IACjC,YAAY,OAAe;QACvB,KAAK,CAAC,OAAO,CAAC,CAAA;QAEd,0BAA0B;QAC1B,IAAI,KAAK,CAAC,iBAAiB,EAAE;YACzB,KAAK,CAAC,iBAAiB,CAAC,IAAI,EAAE,UAAU,CAAC,CAAA;SAC5C;QAED,IAAI,CAAC,IAAI,GAAG,YAAY,CAAA;IAC5B,CAAC;CACJ;AAXD,gCAWC;AAiBD,SAAS,YAAY,CAAO,KAAQ;IAChC,MAAM,QAAQ,GAA0B,EAAE,CAAA;IAC1C,IAAI,GAAG,GAAmC,SAAS,CAAA;IAEnD,MAAM,IAAI,GAAG,UAAU,kBAAmC;QACtD,MAAM,IAAI,GAAG,UAAU,YAAgC;YACnD,QAAQ,CAAC,IAAI,CAAC;gBACV,IAAI,EAAE,kBAAkB;gBACxB,IAAI,EAAE,YAAY;aACrB,CAAC,CAAA;YACF,OAAO,IAAI,CAAA;QACf,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QAEZ,OAAO;YACH,IAAI;SACP,CAAA;IACL,CAAC,CAAA;IACD,MAAM,YAAY,GAAG,UAAU,QAA4B;QACvD,GAAG,GAAG,QAAQ,CAAA;QAEd,OAAO,IAAI,CAAA;IACf,CAAC,CAAA;IAED,MAAM,QAAQ,GAAG;QACb,GAAG,EAAE,GAAG,EAAE;YACN,MAAM,OAAO,GAAG,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAA;YAC7D,IAAI,OAAO,EAAE;gBACT,OAAO,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;aAC7B;iBAAM;gBACH,IAAI,GAAG,EAAE;oBACL,OAAO,GAAG,CAAC,KAAK,CAAC,CAAA;iBACpB;qBAAM;oBACH,MAAM,IAAI,UAAU,CAAC,8BAA8B,KAAK,EAAE,CAAC,CAAA;iBAC9D;aACJ;QACL,CAAC;QACD,IAAI,EAAE,SAAS;QACf,OAAO,EAAE,SAAS;KACrB,CAAA;IAED,QAAQ,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;IACnC,QAAQ,CAAC,OAAO,GAAG,YAAY,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;IAC9C,OAAO,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAuB,CAAA;AACxD,CAAC;AAEY,QAAA,KAAK,GAAa,YAAY,CAAC,IAAI,CAAC,YAAY,CAAC,CAAA"}
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@othree.io/cerillo",
3
- "version": "0.1.0",
3
+ "version": "1.0.0",
4
4
  "description": "Matching library",
5
- "main": "index.js",
5
+ "main": "lib/match.js",
6
6
  "scripts": {
7
- "test": "jest"
7
+ "test": "jest",
8
+ "build": "tsc"
8
9
  },
9
10
  "repository": {
10
11
  "type": "git",
@@ -22,6 +23,9 @@
22
23
  },
23
24
  "homepage": "https://gitlab.com/othree.oss/cerillo#readme",
24
25
  "devDependencies": {
25
- "jest": "^26.5.3"
26
+ "@types/jest": "^27.0.2",
27
+ "jest": "^27.2.1",
28
+ "ts-jest": "^27.0.5",
29
+ "typescript": "^4.4.3"
26
30
  }
27
31
  }
@@ -0,0 +1,4 @@
1
+ module.exports = {
2
+ plugins: ['@semantic-release/commit-analyzer', '@semantic-release/release-notes-generator', '@semantic-release/npm'],
3
+ branches: ['master', 'main']
4
+ }
package/src/match.js DELETED
@@ -1,46 +0,0 @@
1
- function unboundMatch(value) {
2
- const patterns = []
3
- let def = undefined
4
-
5
- const when = function (validationCallback) {
6
- const then = function (thenCallback) {
7
- patterns.push({
8
- when: validationCallback,
9
- then: thenCallback
10
- })
11
- return this
12
- }.bind(this)
13
-
14
- return {
15
- then
16
- }
17
- }
18
- const defaultMatch = function(callback) {
19
- def = callback
20
-
21
- return this
22
- }
23
-
24
- const response = {
25
- get: () => {
26
- const pattern = patterns.find(pattern => pattern.when(value))
27
- if (pattern) {
28
- return pattern.then(value)
29
- } else {
30
- if (def) {
31
- return def(value)
32
- } else {
33
- throw new Error(`Nothing matched for value: ${value}`)
34
- }
35
- }
36
- }
37
- }
38
-
39
- response.when = when.bind(response)
40
- response.default = defaultMatch.bind(response)
41
- return response
42
- }
43
-
44
- const match = unboundMatch.bind(unboundMatch)
45
-
46
- module.exports = { match }
@@ -1,42 +0,0 @@
1
- const {match} = require('../src/match')
2
-
3
- describe('match', () => {
4
-
5
- const expectations = [
6
- {
7
- value: 'bye',
8
- expectation: 'MEANT TO SAY GOODBYE'
9
- },
10
- {
11
- value: 'hello',
12
- expectation: 'HELLO'
13
- },
14
- {
15
- value: 'unexpected',
16
- expectation: 'DEFAULT VALUE'
17
- }
18
- ]
19
-
20
- expectations.forEach(expectation => {
21
- it(`should match ${expectation.value} and return the expected value`, () => {
22
-
23
- const result = match(expectation.value)
24
- .when(_ => _ === 'bye').then(_ => 'MEANT TO SAY GOODBYE')
25
- .when(_ => _ === 'hello').then(_ => _.toUpperCase())
26
- .default(_ => 'DEFAULT VALUE')
27
- .get()
28
-
29
- expect(result).toStrictEqual(expectation.expectation)
30
- })
31
- })
32
-
33
- it('throw an error if there is no match and no default defined', () => {
34
- const value = 'unexpected'
35
- const lazyMatch = match(value)
36
- .when(_ => _ === 'bye').then(_ => 'MEANT TO SAY GOODBYE')
37
- .when(_ => _ === 'hello').then(_ => _.toUpperCase())
38
-
39
- expect(() => lazyMatch.get()).toThrow(`Nothing matched for value: ${value}`)
40
- })
41
-
42
- })