hsin421 / angular-1 Goto Github PK
View Code? Open in Web Editor NEWJasmine Test suite and passing
Jasmine Test suite and passing
Hsin -
Below are some comments on the first assignment. Just some stylistic things to watch out for:
apply.js
var _nycda = _nycda || {};
_nycda.apply = function (item, property, value) {
/* Combine these using an && */
if (item && property) {
/* In your version all the variables below are in global scope.
This is fine for these tests, but dangerous in the course of
normal application development, try to prepend with var */
var prop_parse = property.split(".");
/* small, but you could make this simpler by just referencing
length at each point */
/* Also, you can just throw the first item into the new array */
var myarray = [item];
var ct = 0;
for (var a = 0; a < prop_parse.length; a++) {
if (!item.hasOwnProperty(prop_parse[a])) {
ct++;
if (ct >= 2) throw "Error";
}
else {
myarray[a + 1] = item[prop_parse[a]]
item = item[prop_parse[a]]
}
if (a == prop_parse.length - 1) {
myarray[a + 1] = value;
}
}
for (var b = prop_parse.length - 1; b >= 0; b--) {
myarray[b][prop_parse[b]] = myarray[b + 1]
}
return myarray[0]
}
/* You can just short circuit down here if either of the upper ifs don't pass */
return item
};
get.js
var _nycda = _nycda || {};
_nycda.get = function (item, property) {
if (item && property) {
/* Same comment about avoiding the global variables within the function, use var */
var prop_parse = property.split(".")
for (var a = 0; a < prop_parse.length; a++) {
if (item.hasOwnProperty(prop_parse[a])) {
item = item[prop_parse[a]]
}
else {
return undefined
}
if (a == prop_parse.length - 1) {
return item
}
}
}
return item;
};
/* Another way to cleverly do this one */
_nycda.get = function (item, property) {
if (item && property) {
/* Same comment about avoiding the global variables within the function, use var */
property = property.split(".")
while (property.length) {
if (item) {
item = item[property.shift()];
} else {
break;
}
}
}
return item;
};
conditional.js
var _nycda = _nycda || {};
_nycda.conditional = function (condition, runIfTrue) {
/* May be best to actually check if this is a function, bit more robust */
if (typeof runIfTrue === 'function') {
if (condition == null) {
return runIfTrue;
}
return function () {
if (condition.apply(null, arguments)) {
return runIfTrue.apply(null, arguments)
}
else {
return (function () {
return undefined
})();
}
};
} else {
throw "Hey don't do that";
}
};
/* Otherwise, very good */
Thanks Hsin. You are quite excellent at this. Your apply implementation was very clever, I have never seen that one before.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.