Merge pull request #47 from jodeleeuw/smart-arrays

Smart arrays
This commit is contained in:
Josh de Leeuw 2014-05-21 13:31:42 -04:00
commit d86c82f707
2 changed files with 26 additions and 0 deletions

View File

@ -227,6 +227,12 @@
}; };
//
// These are public functions, intended to be used for developing plugins.
// They aren't considered part of the normal API for the core library.
//
core.normalizeTrialVariables = function(trial, protect){ core.normalizeTrialVariables = function(trial, protect){
protect = (typeof protect === 'undefined') ? [] : protect; protect = (typeof protect === 'undefined') ? [] : protect;
@ -255,6 +261,23 @@
return tmp; return tmp;
} }
// if possible_array is not an array, then return a one-element array
// containing possible_array
core.enforceArray = function(params, possible_arrays) {
// function to check if something is an array, fallback
// to string method if browser doesn't support Array.isArray
var ckArray = Array.isArray || function(a) {
return toString.call(a) == '[object Array]';
}
for(var i=0; i<possible_arrays.length; i++){
params[possible_arrays[i]] = ckArray(params[possible_arrays[i]]) ? params[possible_arrays[i]] : [params[possible_arrays[i]]];
}
return params;
}
// //
// private functions // // private functions //

View File

@ -13,6 +13,9 @@
var plugin = {}; var plugin = {};
plugin.create = function(params) { plugin.create = function(params) {
params = jsPsych.enforceArray(params, ['text','data']);
var trials = new Array(params.text.length); var trials = new Array(params.text.length);
for (var i = 0; i < trials.length; i++) { for (var i = 0; i < trials.length; i++) {
trials[i] = {}; trials[i] = {};