-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
57cd54e
commit f3c8f37
Showing
10 changed files
with
3,667 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
/*core*/ | ||
define(function (require, exports, module) { | ||
var canSet = require('can-set'); | ||
var helpers = require('./helpers/helpers'); | ||
var sub = require('./helpers/sub'); | ||
require('./store'); | ||
var fixtures = []; | ||
exports.fixtures = fixtures; | ||
exports.add = function (settings, fixture) { | ||
if (fixture && (fixture.getData || fixture.getListData)) { | ||
var root = settings, store = fixture, idProp = store.idProp; | ||
fixture = undefined; | ||
settings = {}; | ||
settings['GET ' + root] = store.getData; | ||
settings['DELETE ' + root] = store.destroyData; | ||
settings['PUT ' + root] = store.updateData; | ||
var getListUrl = root.replace(new RegExp('\\/\\{' + idProp + '\\}.*'), ''); | ||
settings['GET ' + getListUrl] = store.getListData; | ||
settings['POST ' + getListUrl] = store.createData; | ||
} | ||
if (fixture !== undefined) { | ||
if (typeof settings === 'string') { | ||
var matches = settings.match(/(GET|POST|PUT|DELETE|PATCH) (.+)/i); | ||
if (!matches) { | ||
settings = { url: settings }; | ||
} else { | ||
settings = { | ||
url: matches[2], | ||
type: matches[1] | ||
}; | ||
} | ||
} | ||
var index = exports.index(settings, true); | ||
if (index > -1) { | ||
fixtures.splice(index, 1); | ||
} | ||
if (fixture == null) { | ||
return; | ||
} | ||
if (typeof fixture === 'object') { | ||
var data = fixture; | ||
fixture = function () { | ||
return data; | ||
}; | ||
} | ||
settings.fixture = fixture; | ||
fixtures.unshift(settings); | ||
} else { | ||
helpers.each(settings, function (fixture, url) { | ||
exports.add(url, fixture); | ||
}); | ||
} | ||
}; | ||
var $fixture = exports.add; | ||
$fixture.on = true; | ||
$fixture.delay = 10; | ||
exports.callDynamicFixture = function (xhrSettings, fixtureSettings, cb) { | ||
xhrSettings.data = fixtureSettings.data; | ||
var response = function () { | ||
var res = exports.extractResponse.apply(xhrSettings, arguments); | ||
return cb.apply(this, res); | ||
}; | ||
var callFixture = function () { | ||
var result = fixtureSettings.fixture(xhrSettings, response, xhrSettings.headers, fixtureSettings); | ||
if (result !== undefined) { | ||
response(200, result); | ||
} | ||
}; | ||
if (!xhrSettings.async) { | ||
callFixture(); | ||
} else { | ||
setTimeout(callFixture, $fixture.delay); | ||
} | ||
}; | ||
exports.index = function (settings, exact) { | ||
for (var i = 0; i < fixtures.length; i++) { | ||
if (exports.matches(settings, fixtures[i], exact)) { | ||
return i; | ||
} | ||
} | ||
return -1; | ||
}; | ||
exports.get = function (xhrSettings) { | ||
if (!$fixture.on) { | ||
return; | ||
} | ||
var index = exports.index(xhrSettings, true); | ||
if (index === -1) { | ||
index = exports.index(xhrSettings, false); | ||
} | ||
var fixtureSettings = index >= 0 ? helpers.extend({}, fixtures[index]) : undefined; | ||
if (fixtureSettings) { | ||
var url = fixtureSettings.fixture, data = exports.dataFromUrl(fixtureSettings.url, xhrSettings.url); | ||
if (typeof fixtureSettings.fixture === 'string') { | ||
if (data) { | ||
url = sub(url, data); | ||
} | ||
fixtureSettings.url = url; | ||
fixtureSettings.data = null; | ||
fixtureSettings.type = 'GET'; | ||
if (!fixtureSettings.error) { | ||
fixtureSettings.error = function (xhr, error, message) { | ||
throw 'fixtures.js Error ' + error + ' ' + message; | ||
}; | ||
} | ||
} else { | ||
var xhrData = helpers.extend({}, xhrSettings.data || {}); | ||
fixtureSettings.data = helpers.extend(xhrData, data); | ||
} | ||
} | ||
return fixtureSettings; | ||
}; | ||
exports.matches = function (settings, fixture, exact) { | ||
if (exact) { | ||
return canSet.equal(settings, fixture, { | ||
fixture: function () { | ||
return true; | ||
} | ||
}); | ||
} else { | ||
return canSet.subset(settings, fixture, exports.defaultCompare); | ||
} | ||
}; | ||
var isEmptyOrNull = function (a, b) { | ||
if (a == null && helpers.isEmptyObject(b)) { | ||
return true; | ||
} else if (b == null && helpers.isEmptyObject(a)) { | ||
return true; | ||
} else { | ||
return canSet.equal(a, b); | ||
} | ||
}; | ||
exports.defaultCompare = { | ||
url: function (a, b) { | ||
return !!exports.dataFromUrl(b, a); | ||
}, | ||
fixture: function () { | ||
return true; | ||
}, | ||
type: function (a, b) { | ||
return b && a ? a.toLowerCase() === b.toLowerCase() : b === a; | ||
}, | ||
helpers: function () { | ||
return true; | ||
}, | ||
headers: isEmptyOrNull, | ||
data: isEmptyOrNull | ||
}; | ||
var replacer = /\{([^\}]+)\}/g; | ||
exports.dataFromUrl = function (fixtureUrl, url) { | ||
var order = [], fixtureUrlAdjusted = fixtureUrl.replace('.', '\\.').replace('?', '\\?'), res = new RegExp(fixtureUrlAdjusted.replace(replacer, function (whole, part) { | ||
order.push(part); | ||
return '([^/]+)'; | ||
}) + '$').exec(url), data = {}; | ||
if (!res) { | ||
return null; | ||
} | ||
res.shift(); | ||
helpers.each(order, function (name) { | ||
data[name] = res.shift(); | ||
}); | ||
return data; | ||
}; | ||
exports.extractResponse = function (status, response, headers, statusText) { | ||
if (typeof status !== 'number') { | ||
headers = response; | ||
response = status; | ||
status = 200; | ||
} | ||
if (typeof headers === 'string') { | ||
statusText = headers; | ||
headers = {}; | ||
} | ||
return [ | ||
status, | ||
response, | ||
headers, | ||
statusText | ||
]; | ||
}; | ||
exports.log = function () { | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/*fixture*/ | ||
define(function (require, exports, module) { | ||
var core = require('./core'); | ||
var fixture = core.add; | ||
var helpers = require('./helpers/helpers'); | ||
var Store = require('./store'); | ||
require('./xhr'); | ||
var noop = function () { | ||
}; | ||
helpers.extend(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)); | ||
} | ||
} | ||
var choices = arr.slice(0); | ||
if (min === undefined) { | ||
min = 1; | ||
max = choices.length; | ||
} else if (max === undefined) { | ||
max = min; | ||
} | ||
var result = []; | ||
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 helpers.extend({}, { | ||
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' && !require.resolve) { | ||
window.fixture = fixture; | ||
} | ||
module.exports = fixture; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/*helpers/deparam*/ | ||
define(function (require, exports, module) { | ||
var helpers = require('./helpers'); | ||
var digitTest = /^\d+$/, keyBreaker = /([^\[\]]+)|(\[\])/g, paramTest = /([^?#]*)(#.*)?$/, prep = function (str) { | ||
return decodeURIComponent(str.replace(/\+/g, ' ')); | ||
}; | ||
module.exports = function (params) { | ||
var data = {}, pairs, lastPart; | ||
if (params && paramTest.test(params)) { | ||
pairs = params.split('&'); | ||
helpers.each(pairs, function (pair) { | ||
var parts = pair.split('='), key = prep(parts.shift()), value = prep(parts.join('=')), current = data; | ||
if (key) { | ||
parts = key.match(keyBreaker); | ||
for (var j = 0, l = parts.length - 1; j < l; j++) { | ||
if (!current[parts[j]]) { | ||
current[parts[j]] = digitTest.test(parts[j + 1]) || parts[j + 1] === '[]' ? [] : {}; | ||
} | ||
current = current[parts[j]]; | ||
} | ||
lastPart = parts.pop(); | ||
if (lastPart === '[]') { | ||
current.push(value); | ||
} else { | ||
current[lastPart] = value; | ||
} | ||
} | ||
}); | ||
} | ||
return data; | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/*helpers/getid*/ | ||
define(function (require, exports, module) { | ||
module.exports = function (xhrSettings, fixtureSettings) { | ||
var id = xhrSettings.data.id; | ||
if (id === undefined && typeof xhrSettings.data === 'number') { | ||
id = xhrSettings.data; | ||
} | ||
if (id === undefined) { | ||
xhrSettings.url.replace(/\/(\d+)(\/|$|\.)/g, function (all, num) { | ||
id = num; | ||
}); | ||
} | ||
if (id === undefined) { | ||
id = xhrSettings.url.replace(/\/(\w+)(\/|$|\.)/g, function (all, num) { | ||
if (num !== 'update') { | ||
id = num; | ||
} | ||
}); | ||
} | ||
if (id === undefined) { | ||
id = Math.round(Math.random() * 1000); | ||
} | ||
return id; | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/*helpers/helpers*/ | ||
define(function (require, exports, module) { | ||
var helpers = { | ||
extend: function (d, s) { | ||
for (var prop in s) { | ||
d[prop] = s[prop]; | ||
} | ||
return d; | ||
}, | ||
isArrayLike: function (obj) { | ||
return obj && typeof obj.length === 'number' && obj.length >= 0; | ||
}, | ||
each: function (obj, cb) { | ||
if (helpers.isArrayLike(obj)) { | ||
for (var i = 0; i < obj.length; i++) { | ||
cb(obj[i], i); | ||
} | ||
} else { | ||
for (var prop in obj) { | ||
cb(obj[prop], prop); | ||
} | ||
} | ||
return obj; | ||
}, | ||
isEmptyObject: function (obj) { | ||
for (var prop in obj) { | ||
return false; | ||
} | ||
return true; | ||
}, | ||
firstProp: function (obj) { | ||
for (var prop in obj) { | ||
return prop; | ||
} | ||
} | ||
}; | ||
module.exports = helpers; | ||
}); |
Oops, something went wrong.