-
Notifications
You must be signed in to change notification settings - Fork 5
/
fixture.js
80 lines (70 loc) · 1.8 KB
/
fixture.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"use strict";
var core = require("./core");
var fixture = core.add;
var Store = require("./store");
require("./xhr");
var canReflect = require("can-reflect");
var canDev = require("can-log/dev/dev");
var ns = require("can-namespace");
// HELPERS START
var noop = function(){};
canReflect.assignMap(fixture, {
rand: function randomize (arr, min, max) {
if (typeof arr === 'number') {
if (typeof min === 'number') {
return arr + Math.floor(Math.random() * (min - arr+1));
} else {
return Math.floor(Math.random() * (arr+1));
}
}
// clone the array because we will remove items from it.
var choices = arr.slice(0);
// get a random set
if (min === undefined) {
min = 1;
max = choices.length;
} else if(max === undefined){
max = min;
}
// get a random selection of arr
var result = [];
// set max
//random max
var selectedCount = min + Math.round(randomize(max - min));
for (var i = 0; i < selectedCount; i++) {
var selectedIndex = randomize(choices.length - 1),
selected = choices.splice(selectedIndex, 1)[0];
result.push(selected);
}
return result;
},
xhr: function (xhr) {
return canReflect.assignMap({}, {
abort: noop,
getAllResponseHeaders: function () {
return "";
},
getResponseHeader: function () {
return "";
},
open: noop,
overrideMimeType: noop,
readyState: 4,
responseText: "",
responseXML: null,
send: noop,
setRequestHeader: noop,
status: 200,
statusText: "OK"
}, xhr);
},
store: Store.make,
fixtures: core.fixtures
});
if(typeof window !== "undefined" && typeof require.resolve !== "function") {
window.fixture = function(){
canDev.warn("You are using the global fixture. Make sure you import can-fixture.");
return fixture.apply(this, arguments);
};
}
module.exports = ns.fixture = fixture;