{"id":20983843,"url":"https://github.com/derhowie/type-switch","last_synced_at":"2025-12-29T23:09:00.720Z","repository":{"id":57383256,"uuid":"61926747","full_name":"derHowie/type-switch","owner":"derHowie","description":"A portable signal engine used to power games and interactive experiences with keypress events","archived":false,"fork":false,"pushed_at":"2016-08-14T19:35:32.000Z","size":194,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-20T07:10:02.739Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/derHowie.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-06-25T04:23:50.000Z","updated_at":"2018-09-15T14:09:18.000Z","dependencies_parsed_at":"2022-09-13T23:51:54.457Z","dependency_job_id":null,"html_url":"https://github.com/derHowie/type-switch","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derHowie%2Ftype-switch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derHowie%2Ftype-switch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derHowie%2Ftype-switch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derHowie%2Ftype-switch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/derHowie","download_url":"https://codeload.github.com/derHowie/type-switch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243392299,"owners_count":20283560,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-19T05:50:28.402Z","updated_at":"2025-12-29T23:09:00.692Z","avatar_url":"https://github.com/derHowie.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# type-switch\nA portable signal engine used to power games and interactive experiences with keypress events.\n\nSee an example [here](https://derhowie.github.io/type-switch-default)\n\n## Installation\n```\nnpm install --save 'type-switch'\n```\n\n## Usage\n```js\nvar TypeSwitch = require('type-switch');\n\nvar myTypeSwitch = new TypeSwitch();\n```\n\n## API\n\n### TypeSwitch({options})\n\n#### options\n\nType: `object`\n\nOptions set here will override the defaults in the constructor.\n\n##### stubbornMode\n\nOne of the larger distinctions among typing games is whether or not an incorrect input will advance the user's position in the prompt. Setting stubbornMode to true will force the user to type the correct character before advancing further.\n\nType: `boolean`\nDefault: `false`\n\n### typeSwitch.getGameStats()\n\nreturns an object containing your instance's current game stats\n\n```js\nmyTypeSwitch.getGameStats();\n\n/*{\n  prompt: 'Hey Mr. User, type this prompt!',\n  time: 23,\n  paused: false,\n  currentIndex: 5,\n  lastExpectedInput: 'M',\n  lastUserInput: 'M',\n  result: 'correct',\n  incorrectTotal: 2\n  }*/\n```\n##### prompt\n\nthe string supplied to your type-switch instance via typeSwitch.start() or typeSwitch.changePrompt\n\n##### time\n\nthe time elapsed since typeSwitch.start() was called, unless it has been changed manually with typeSwitch.changeTime()\n\n##### paused\n\nif false, time is not incrementing and your instance is not listening for the keypress events\n\n##### currentIndex\n\nThis is not the index when the key was pressed, but rather the index of the prompt resulting from the last keystroke. So in the case above, the user was at prompt[4] when they typed the correct answer leading to a currentIndex of 5.\n\n##### lastExpectedInput\n\nthe correct answer\n\n##### lastUserInput\n\nthe user's last answer\n\n##### result\n\nthis provides a string describing the result of the last keypress\n\n##### incorrectTotal\n\nthe number of incorrect keystrokes since typeSwitch.start() was called\n\n### typeSwitch.start(prompt)\n\nbegins game clock and accepts a prompt for the user to type\n\n#### prompt\n\nType: `string`\n\n### typeSwitch.changeCurrentIndex(newIndex)\n\nchanges the users position in the game prompt\n\n#### newIndex\n\n*Required*\n\nType: `number`\n\n### typeSwitch.changeTime(newTime)\n\nchanges the game time to provided value\n\n#### newTime\n\n*Required*\n\nType: `number`\n\n### typeSwitch.changePrompt(newPrompt)\n\nused to change the user's prompt mid-game\n\n#### newPrompt\n\n*Required*\n\nType: `string`\n\n### typeSwitch.pauseGame()\n\npauses game clock and causes type-switch to refrain from listening to keystrokes\n\n### typeSwitch.resumeGame()\n\nresumes game clock and informs type-switch to continue listening for keystrokes\n\n### typeSwitch.pauseGameClock()\n\npauses game clock, but type-switch continues to listen for keystrokes\n\n### typeSwitch.resumeGameClock()\n\nresumes game clock, but does not reinstitute type-switch's keypress event listener\n\n### typeSwitch.restartGame()\n\nThis method resets the user's current index, game clock, and incorrect total, then begins the game anew with the most recently provided prompt.\n\n### typeSwitch.resetGame()\n\nThis method is useful if you want to restart the game, but have some other code you want to run beforehand. All game stats will be reset except the most recent prompt and the game will not immediately begin again.\n\n### typeSwitch.broadcast(event)\n\ntype-switch is an EventEmitter. Use this method to create your own custom hooks.\n\n#### event\n\n*Required*\n\nType: `string`\n\ntype-switch has 3 hooks by default: 'correct', 'incorrect', and 'complete'. Calling typeSwitch.getGameStats() within these hooks is a powerful way of monitoring the user's progress and running your own code in synchrony with each keypress.\n\n```js\nvar myTypeSwitch = new TypeSwitch();\nvar points = 0;\nvar failureMessage = 'Oh man! You stink! You should have typed ';\nvar successMessage = 'Great job, good sir! *tips hat*';\nfunction partyTime() {...};\n\nmyTypeSwitch.on('incorrect', function() {\n  var stats = myTypeSwitch.getGameStats();\n  if (stats.incorrectTotal \u003e 4) {\n    alert(failureMessage + stats.lastExpectedInput + ', but you typed ' + stats.lastUserInput + ', time to start over!');\n    points = 0;\n    myTypeSwitch.restartGame();\n  }\n});\n\nmyTypeSwitch.on('correct', function() {\n  points++;\n});\n\nmyTypeSwitch.on('complete', function() {\n  alert(successMessage);\n  partyTime();\n});\n```\n\n## License\n\nMIT © [Christopher Howard]\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fderhowie%2Ftype-switch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fderhowie%2Ftype-switch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fderhowie%2Ftype-switch/lists"}