UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

138 lines (137 loc) 4.75 kB
function _array_like_to_array(arr, len) { if (len == null || len > arr.length) len = arr.length; for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i]; return arr2; } function _array_with_holes(arr) { if (Array.isArray(arr)) return arr; } function _array_without_holes(arr) { if (Array.isArray(arr)) return _array_like_to_array(arr); } function _iterable_to_array(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _iterable_to_array_limit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){ _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally{ try { if (!_n && _i["return"] != null) _i["return"](); } finally{ if (_d) throw _e; } } return _arr; } function _non_iterable_rest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _non_iterable_spread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _sliced_to_array(arr, i) { return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest(); } function _to_consumable_array(arr) { return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread(); } function _unsupported_iterable_to_array(o, minLen) { if (!o) return; if (typeof o === "string") return _array_like_to_array(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen); } import times from 'lodash/fp/times'; import concat from 'lodash/fp/concat'; import map from 'lodash/fp/map'; import reduce from 'lodash/fp/reduce'; import range from 'lodash/fp/range'; import flatten from 'lodash/fp/flatten'; export function identityMatrix(param, size) { var _param = _sliced_to_array(param, 2), tmp = _param[0], a = tmp === void 0 ? 1 : tmp, tmp1 = _param[1], b = tmp1 === void 0 ? 0 : tmp1; return map(function(row) { return times(function(n) { return n === row ? a : b; }, size); }, range(0, size)); } // same concept as https://github.com/dankogai/js-combinatorics#cartesian-product export function cartesianProduct() { for(var _len = arguments.length, arrays = new Array(_len), _key = 0; _key < _len; _key++){ arrays[_key] = arguments[_key]; } return reduce(function(a, b) { return flatten(map(function(x) { return map(function(y) { return concat(x, [ y ]); }, b); }, a)); }, [ [] ], arrays); } // same concept as https://github.com/dankogai/js-combinatorics#base-n export function baseNCombination(values, n) { if (n === 0) { return []; } if (n === 1) { return map(function(v) { return [ v ]; }, values); } var previous = baseNCombination(values, n - 1); return cartesianProduct(values, previous).map(function(param) { var _param = _sliced_to_array(param, 2), v = _param[0], p = _param[1]; return [ v ].concat(_to_consumable_array(p)); }); } // same concept as https://github.com/dankogai/js-combinatorics#combination export function combination(array, size) { if (size === 0) { return []; } if (size === 1) { return map(function(v) { return [ v ]; }, array); } if (array.length === 1) { return [ times(function() { return array[0]; }, size) ]; } if (array.length === 0) { return []; } return concat(map(function(v) { return [ array[0] ].concat(_to_consumable_array(v)); }, combination(array, size - 1)), combination(array.slice(1), size)); }