Skip to content

Commit

Permalink
4.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
justinbmeyer committed Jan 29, 2018
1 parent 84ac5ef commit c1e9826
Show file tree
Hide file tree
Showing 3 changed files with 489 additions and 0 deletions.
122 changes: 122 additions & 0 deletions dist/amd/can-stache-converters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*can-stache-converters@4.0.0-pre.0#can-stache-converters*/
define([
'require',
'exports',
'module',
'can-reflect',
'can-stache',
'can-util/js/string-to-any',
'can-util/js/make-array',
'can-util/js/dev',
'can-stache-bindings'
], function (require, exports, module) {
var canReflect = require('can-reflect');
var stache = require('can-stache');
var stringToAny = require('can-util/js/string-to-any');
var makeArray = require('can-util/js/make-array');
var dev = require('can-util/js/dev');
require('can-stache-bindings');
stache.registerConverter('boolean-to-inList', {
get: function (item, list) {
if (!list) {
return false;
} else {
return list.indexOf(item) !== -1;
}
},
set: function (newVal, item, list) {
if (!list) {
return;
}
if (!newVal) {
var idx = list.indexOf(item);
if (idx !== -1) {
list.splice(idx, 1);
}
} else {
list.push(item);
}
}
});
stache.addConverter('string-to-any', {
get: function (obs) {
return '' + canReflect.getValue(obs);
},
set: function (newVal, obs) {
var converted = stringToAny(newVal);
canReflect.setValue(obs, converted);
}
});
stache.addConverter('not', {
get: function (obs) {
return !canReflect.getValue(obs);
},
set: function (newVal, obs) {
canReflect.setValue(obs, !newVal);
}
});
stache.addConverter('index-to-selected', {
get: function (item, list) {
var val = canReflect.getValue(item);
var idx = canReflect.getValue(list).indexOf(val);
return idx;
},
set: function (idx, item, list) {
var newVal = canReflect.getValue(list)[idx];
canReflect.setValue(item, newVal);
}
});
stache.addConverter('selected-to-index', {
get: function (idx, list) {
var val = canReflect.getValue(idx), listValue = canReflect.getValue(list);
var item = listValue[val];
return item;
},
set: function (item, idx, list) {
var newVal = canReflect.getValue(list).indexOf(item);
canReflect.setValue(idx, newVal);
}
});
stache.addConverter('either-or', {
get: function (chosen, a, b) {
var chosenVal = canReflect.getValue(chosen), aValue = canReflect.getValue(a), bValue = canReflect.getValue(b);
var matchA = aValue === chosenVal;
var matchB = bValue === chosenVal;
if (!matchA && !matchB) {
return;
} else {
return matchA;
}
},
set: function (newVal, chosen, a, b) {
var setVal = newVal ? canReflect.getValue(a) : canReflect.getValue(b);
canReflect.setValue(chosen, setVal);
}
});
stache.addConverter('equal', {
get: function () {
var args = makeArray(arguments);
args.pop();
if (args.length > 1) {
var comparer = canReflect.getValue(args.pop());
return args.every(function (obs) {
var value = canReflect.getValue(obs);
return value === comparer;
});
}
},
set: function () {
var args = makeArray(arguments);
args.pop();
if (args.length > 2) {
var b = args.shift();
var comparer = args.pop();
if (b) {
for (var i = 0; i < args.length; i++) {
canReflect.setValue(args[i], comparer);
}
}
}
}
});
});
110 changes: 110 additions & 0 deletions dist/cjs/can-stache-converters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*can-stache-converters@4.0.0-pre.0#can-stache-converters*/
var canReflect = require('can-reflect');
var stache = require('can-stache');
var stringToAny = require('can-util/js/string-to-any/string-to-any');
var makeArray = require('can-util/js/make-array/make-array');
var dev = require('can-util/js/dev/dev');
require('can-stache-bindings');
stache.registerConverter('boolean-to-inList', {
get: function (item, list) {
if (!list) {
return false;
} else {
return list.indexOf(item) !== -1;
}
},
set: function (newVal, item, list) {
if (!list) {
return;
}
if (!newVal) {
var idx = list.indexOf(item);
if (idx !== -1) {
list.splice(idx, 1);
}
} else {
list.push(item);
}
}
});
stache.addConverter('string-to-any', {
get: function (obs) {
return '' + canReflect.getValue(obs);
},
set: function (newVal, obs) {
var converted = stringToAny(newVal);
canReflect.setValue(obs, converted);
}
});
stache.addConverter('not', {
get: function (obs) {
return !canReflect.getValue(obs);
},
set: function (newVal, obs) {
canReflect.setValue(obs, !newVal);
}
});
stache.addConverter('index-to-selected', {
get: function (item, list) {
var val = canReflect.getValue(item);
var idx = canReflect.getValue(list).indexOf(val);
return idx;
},
set: function (idx, item, list) {
var newVal = canReflect.getValue(list)[idx];
canReflect.setValue(item, newVal);
}
});
stache.addConverter('selected-to-index', {
get: function (idx, list) {
var val = canReflect.getValue(idx), listValue = canReflect.getValue(list);
var item = listValue[val];
return item;
},
set: function (item, idx, list) {
var newVal = canReflect.getValue(list).indexOf(item);
canReflect.setValue(idx, newVal);
}
});
stache.addConverter('either-or', {
get: function (chosen, a, b) {
var chosenVal = canReflect.getValue(chosen), aValue = canReflect.getValue(a), bValue = canReflect.getValue(b);
var matchA = aValue === chosenVal;
var matchB = bValue === chosenVal;
if (!matchA && !matchB) {
return;
} else {
return matchA;
}
},
set: function (newVal, chosen, a, b) {
var setVal = newVal ? canReflect.getValue(a) : canReflect.getValue(b);
canReflect.setValue(chosen, setVal);
}
});
stache.addConverter('equal', {
get: function () {
var args = makeArray(arguments);
args.pop();
if (args.length > 1) {
var comparer = canReflect.getValue(args.pop());
return args.every(function (obs) {
var value = canReflect.getValue(obs);
return value === comparer;
});
}
},
set: function () {
var args = makeArray(arguments);
args.pop();
if (args.length > 2) {
var b = args.shift();
var comparer = args.pop();
if (b) {
for (var i = 0; i < args.length; i++) {
canReflect.setValue(args[i], comparer);
}
}
}
}
});
Loading

0 comments on commit c1e9826

Please sign in to comment.