-
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
40a4b03
commit 07c7e6a
Showing
8 changed files
with
3,973 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,203 @@ | ||
/*[email protected]#core*/ | ||
define(function (require, exports, module) { | ||
var canSet = require('can-set'); | ||
var sub = require('can-util/js/string').sub; | ||
var each = require('can-util/js/each'); | ||
var assign = require('can-util/js/assign'); | ||
var isEmptyObject = require('can-util/js/is-empty-object'); | ||
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, itemRegex = new RegExp('\\/\\{' + idProp + '\\}.*'), rootIsItemUrl = itemRegex.test(root), getListUrl = rootIsItemUrl ? root.replace(itemRegex, '') : root, getItemUrl = rootIsItemUrl ? root : root.trim() + '/{' + idProp + '}'; | ||
fixture = undefined; | ||
settings = {}; | ||
settings['GET ' + getItemUrl] = store.getData; | ||
settings['DELETE ' + getItemUrl] = store.destroyData; | ||
settings['PUT ' + getItemUrl] = store.updateData; | ||
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 { | ||
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(); | ||
return null; | ||
} else { | ||
return 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 ? assign({}, 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 = assign({}, xhrSettings.data || {}); | ||
fixtureSettings.data = assign(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 && isEmptyObject(b)) { | ||
return true; | ||
} else if (b == null && isEmptyObject(a)) { | ||
return true; | ||
} else { | ||
return canSet.equal(a, b); | ||
} | ||
}; | ||
var isEmptyOrSubset = function (a, b) { | ||
if (a == null && isEmptyObject(b)) { | ||
return true; | ||
} else if (b == null && isEmptyObject(a)) { | ||
return true; | ||
} else { | ||
return canSet.subset(a, b); | ||
} | ||
}; | ||
exports.defaultCompare = { | ||
url: function (a, b) { | ||
return !!exports.dataFromUrl(b, a); | ||
}, | ||
fixture: function () { | ||
return true; | ||
}, | ||
xhr: function () { | ||
return true; | ||
}, | ||
type: function (a, b) { | ||
return b && a ? a.toLowerCase() === b.toLowerCase() : b === a; | ||
}, | ||
method: function (a, b) { | ||
return b && a ? a.toLowerCase() === b.toLowerCase() : b === a; | ||
}, | ||
helpers: function () { | ||
return true; | ||
}, | ||
headers: isEmptyOrNull, | ||
data: isEmptyOrSubset | ||
}; | ||
var replacer = /\{([^\}]+)\}/g; | ||
exports.dataFromUrl = function (fixtureUrl, url) { | ||
if (!fixtureUrl) { | ||
return {}; | ||
} | ||
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(); | ||
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,62 @@ | ||
/*[email protected]#fixture*/ | ||
define(function (require, exports, module) { | ||
var core = require('./core'); | ||
var fixture = core.add; | ||
var Store = require('./store'); | ||
require('./xhr'); | ||
var assign = require('can-util/js/assign'); | ||
var ns = require('can-namespace'); | ||
var noop = function () { | ||
}; | ||
assign(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 assign({}, { | ||
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 = fixture; | ||
} | ||
module.exports = ns.fixture = 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 @@ | ||
/*[email protected]#helpers/deparam*/ | ||
define(function (require, exports, module) { | ||
var each = require('can-util/js/each'); | ||
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('&'); | ||
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 @@ | ||
/*[email protected]#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; | ||
}; | ||
}); |
Oops, something went wrong.