Skip to content

Commit

Permalink
Merge branch 'v12-umd'
Browse files Browse the repository at this point in the history
  • Loading branch information
TalAter committed Mar 31, 2016
2 parents 0eb4def + c7cc9b6 commit 6dfe17f
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 35 deletions.
7 changes: 5 additions & 2 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@
"sub" : true,
"strict" : true,
"white" : false,
"indent" : 2
}
"indent" : 2,
"globals" : {
"define" : false
}
}
19 changes: 17 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,27 @@ module.exports = function(grunt) {
}
},
jasmine: {
browserAMD: {
src: ['annyang.js'],
options: {
specs: 'test/spec/*Spec.js',
outfile: 'test/SpecRunner.html',
vendor: ['test/vendor/corti.js', 'test/init_corti.js'],
keepRunner: true,
template: require('grunt-template-jasmine-requirejs'),
templateOptions: {
requireConfig: {
baseUrl: '../'
}
}
}
},
testAndCoverage: {
src: ['annyang.js'],
options: {
specs: ['test/spec/*Spec.js'],
outfile: 'test/SpecRunner.html',
polyfills: ['test/vendor/corti.js', 'test/init_corti.js'],
vendor: ['test/vendor/corti.js', 'test/init_corti.js'],
keepRunner: true,
template: require('grunt-template-jasmine-istanbul'),
templateOptions: {
Expand All @@ -94,7 +109,7 @@ module.exports = function(grunt) {
thresholds: {
statements: 80,
branches: 65,
functions: 95,
functions: 90,
lines: 80
}
}
Expand Down
48 changes: 29 additions & 19 deletions annyang.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,18 @@
//! author : Tal Ater @TalAter
//! license : MIT
//! https://www.TalAter.com/annyang/

(function(undefined) {
(function (root, factory) {
"use strict";
if (typeof define === 'function' && define.amd) { // AMD + global
define([], function () {
return (root.annyang = factory(root));
});
} else if (typeof module === 'object' && module.exports) { // CommonJS
module.exports = factory(root);
} else { // Browser globals
root.annyang = factory(root);
}
}(typeof window !== 'undefined' ? window : this, function (root, undefined) {
"use strict";

/**
Expand All @@ -17,8 +27,7 @@
* # API Reference
*/

// Save a reference to the global object (window in the browser)
var root = this;
var annyang;

// Get the SpeechRecognition object, while handling browser prefixes
var SpeechRecognition = root.SpeechRecognition ||
Expand All @@ -30,8 +39,7 @@
// Check browser support
// This is done as early as possible, to make it as fast as possible for unsupported browsers
if (!SpeechRecognition) {
root.annyang = null;
return undefined;
return null;
}

var commandsList = [];
Expand Down Expand Up @@ -75,18 +83,18 @@

var initIfNeeded = function() {
if (!isInitialized()) {
root.annyang.init({}, false);
annyang.init({}, false);
}
};

var registerCommand = function(command, cb, phrase) {
commandsList.push({ command: command, callback: cb, originalPhrase: phrase });
if (debugState) {
root.console.log('Command successfully loaded: %c'+phrase, debugStyle);
console.log('Command successfully loaded: %c'+phrase, debugStyle);
}
};

root.annyang = {
annyang = {

/**
* Initialize annyang with a list of commands to recognize.
Expand Down Expand Up @@ -169,17 +177,17 @@
// play nicely with the browser, and never restart annyang automatically more than once per second
var timeSinceLastStart = new Date().getTime()-lastStartedAt;
if (timeSinceLastStart < 1000) {
setTimeout(root.annyang.start, 1000-timeSinceLastStart);
setTimeout(annyang.start, 1000-timeSinceLastStart);
} else {
root.annyang.start();
annyang.start();
}
}
};

recognition.onresult = function(event) {
if(pauseListening) {
if (debugState) {
root.console.log('Speech heard, but annyang is paused');
console.log('Speech heard, but annyang is paused');
}
return false;
}
Expand All @@ -198,7 +206,7 @@
// the text recognized
commandText = results[i].trim();
if (debugState) {
root.console.log('Speech recognized: %c'+commandText, debugStyle);
console.log('Speech recognized: %c'+commandText, debugStyle);
}

// try and match recognized text to one of the commands on the list
Expand All @@ -208,9 +216,9 @@
if (result) {
var parameters = result.slice(1);
if (debugState) {
root.console.log('command matched: %c'+currentCommand.originalPhrase, debugStyle);
console.log('command matched: %c'+currentCommand.originalPhrase, debugStyle);
if (parameters.length) {
root.console.log('with parameters', parameters);
console.log('with parameters', parameters);
}
}
// execute the matched command
Expand Down Expand Up @@ -270,7 +278,7 @@
recognition.start();
} catch(e) {
if (debugState) {
root.console.log(e.message);
console.log(e.message);
}
}
},
Expand Down Expand Up @@ -309,7 +317,7 @@
* @method resume
*/
resume: function() {
root.annyang.start();
annyang.start();
},

/**
Expand Down Expand Up @@ -371,7 +379,7 @@
registerCommand(new RegExp(cb.regexp.source, 'i'), cb.callback, phrase);
} else {
if (debugState) {
root.console.log('Can not register command: %c'+phrase, debugStyle);
console.log('Can not register command: %c'+phrase, debugStyle);
}
continue;
}
Expand Down Expand Up @@ -544,7 +552,9 @@
}
};

}).call(this);
return annyang;

}));

/**
* # Good to Know
Expand Down
2 changes: 1 addition & 1 deletion annyang.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"grunt-contrib-watch": "^1.0.0",
"grunt-contrib-jasmine": "^1.0.0",
"grunt-template-jasmine-istanbul": "^0.4.0",
"grunt-template-jasmine-requirejs": "^0.2.3",
"grunt-markdox": "^1.2.1"
}
}
2 changes: 1 addition & 1 deletion sites/facebook.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6dfe17f

Please sign in to comment.