Skip to content

Commit

Permalink
feat: Implement matcher type
Browse files Browse the repository at this point in the history
  • Loading branch information
fletcherist committed Jul 8, 2018
1 parent 8cac580 commit 05f174d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 15 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
<a name="1.3.3"></a>
## [1.3.3](https://github.com/fletcherist/yandex-dialogs-sdk/compare/v1.3.2...v1.3.3) (2018-07-08)


### Bug Fixes

* bug with commands (array) ([8cac580](https://github.com/fletcherist/yandex-dialogs-sdk/commit/8cac580))
* package.json ([169a818](https://github.com/fletcherist/yandex-dialogs-sdk/commit/169a818))


### Features

* add guess number game to examples ([3d34c26](https://github.com/fletcherist/yandex-dialogs-sdk/commit/3d34c26))
* add youtube link to readme ([31c8328](https://github.com/fletcherist/yandex-dialogs-sdk/commit/31c8328))



<a name="1.3.2"></a>
## [1.3.2](https://github.com/fletcherist/yandex-dialogs-sdk/compare/v1.2.1...v1.3.2) (2018-07-08)

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{
"name": "yandex-dialogs-sdk",
"version": "1.3.2",
"version": "1.3.3",
"description": "Build your skill for Alice with ease.",
"main": "dist/index.js",
"scripts": {
"lint": "eslint src",
"test": "jest",
"dev": "tsc -w",
"build": "tsc",
"version": "npm run changelog && git add CHANGELOG.md && conventional-github-releaser -p angular",
"version": "npm run changelog && git add CHANGELOG.md",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0",
"release": "conventional-github-releaser -p angular",
"deploy": "tsc && git push --follow-tags origin master && npm publish"
},
"repository": {
Expand Down
29 changes: 19 additions & 10 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@ import {
TYPE_FIGURE,
TYPE_REGEXP,
TYPE_ARRAY,
TYPE_MATCHER,
} from './constants'
import Ctx from './ctx'
import { CommandInterface, CallbackType, CommandType } from './types/command'
import {
CommandInterface,
CallbackType,
CommandType,
CommandNameType,
} from './types/command'
import { CtxInterface } from './types/ctx'

export default class Command implements CommandInterface {
public name: any[] | string | RegExp
public type: | CommandType
public name: CommandNameType
public type: CommandType
public callback: CallbackType

constructor(name: string, callback: CallbackType) {
constructor(name: CommandNameType, callback: CallbackType) {
if (name === undefined) { throw new Error('Command name is not specified') }
this.name = name
this.callback = callback
Expand All @@ -25,15 +31,18 @@ export default class Command implements CommandInterface {
public _defineCommandType(name) {
let type

if (typeof name === 'string') {
type = TYPE_STRING
if (name.includes('${')) {
type = TYPE_FIGURE
if (typeof name === 'function') {
type = TYPE_MATCHER
}
if (typeof name === 'string') {
type = TYPE_STRING
if (name.includes('${')) {
type = TYPE_FIGURE
}
} else if (name instanceof RegExp) {
type = TYPE_REGEXP
type = TYPE_REGEXP
} else if (Array.isArray(name)) {
type = TYPE_ARRAY
type = TYPE_ARRAY
} else {
throw new Error(`Command name is not of proper type.
Could be only string, array of strings or regular expression`)
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export const TYPE_STRING = 'string'
export const TYPE_FIGURE = 'figure'
export const TYPE_REGEXP = 'regexp'
export const TYPE_ARRAY = 'array'
export const TYPE_MATCHER = 'matcher'

export const ALICE_PROTOCOL_VERSION = '1.0'
export const DEFAULT_END_SESSION = false
Expand Down
7 changes: 4 additions & 3 deletions src/types/command.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import Ctx from '../ctx'
import { CtxInterface } from './ctx'

export type CallbackType = (ctx: CtxInterface) => CtxInterface

export type CommandType = 'string' | 'figure' | 'regexp' | 'array'
export type MatcherType = (ctx: CtxInterface) => boolean
export type CommandNameType = any[] | string | RegExp | MatcherType
export type CommandType = 'string' | 'figure' | 'regexp' | 'array' | 'matcher'
export interface CommandInterface {
callback: CallbackType
name: any[] | string | RegExp
name: CommandNameType
type: CommandType
}

0 comments on commit 05f174d

Please sign in to comment.