-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
4 changed files
with
346 additions
and
1 deletion.
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,101 @@ | ||
// You can use JavaScript functions to create local scopes and objects to represent module interfaces | ||
|
||
const weekDay = function(){ | ||
const names = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]; | ||
|
||
return{ | ||
name(number) { return names[number]; }, | ||
number(name) {return names.indexOf(name);} | ||
}; | ||
}(); | ||
|
||
console.log(weekDay.name(weekDay.number("Sunday"))); | ||
// -> Sunday | ||
|
||
|
||
|
||
// Special operator eval will execute a string in the current scope. | ||
// This is usually a bad idea because it breaks some properties that scopes have, such easy predictability. | ||
|
||
const x = 1; | ||
function evalAndReturnX(code) { | ||
eval(code); | ||
return x; | ||
} | ||
|
||
console.log(evalAndReturnX("var x = 2")); | ||
// -> 2 | ||
console.log(x); | ||
// -> 1 | ||
|
||
|
||
// A less scary way of interpreting data as code is to use the Function constructor. | ||
let plusOne = Function("n", "return n + 1;"); | ||
console.log(plusOne(4)); | ||
// -> 5 | ||
|
||
|
||
|
||
// The most widely used approach is called CommonJS modules. | ||
// Node.js uses it and is the system used by most packages on NPM. | ||
const ordinal = require("ordinal"); | ||
const {days, months} = require("date-names"); | ||
|
||
exports.formatDate = function(date, format) { | ||
return format.replace(/YYYY|M(MMM)?|Do?|dddd/g, tag => { | ||
if (tag == "YYYY") return date.getFullYear(); | ||
if (tag == "M") return date.getMonth(); | ||
if (tag == "MMMM") return months[date.getMonth()]; | ||
if (tag == "D") return date.getDate(); | ||
if (tag == "Do") return ordinal(date.getDate()); | ||
if (tag == "dddd") return days[date.getDay()]; | ||
}); | ||
}; | ||
|
||
const {formatDate} = require("./format-date"); | ||
|
||
console.log(formatDate(new Date(2019, 8, 13), | ||
"dddd the Do")); | ||
// → Friday the 13th | ||
|
||
|
||
// We can define require, in its most minimal form, like this: | ||
require.cache = Object.create(null); | ||
|
||
function require(name) { | ||
if (!(name in require.cache)) { | ||
let code = readFile(name); | ||
let module = {exports: {}}; | ||
require.cache[name] = module; | ||
let wrapper = Function("require, exports, module", code); | ||
wrapper(require, module.exports, module); | ||
} | ||
return require.cache[name].exports; | ||
} | ||
|
||
|
||
const {parse} = require("ini"); | ||
|
||
console.log(parse("x = 10\ny = 20")); | ||
// → {x: "10", y: "20"} | ||
|
||
|
||
// ES stands for ECMAScript. | ||
// The notation is now integrated into the language. | ||
// Instead of calling a function to access a dependency, you use a special import keyword. | ||
|
||
import ordinal from "ordinal"; | ||
import {days, months} from "date-names"; | ||
|
||
export function formatDate(date, format) { /* ... */ } | ||
|
||
|
||
// The export keyword is used to export things. | ||
// It may appear in front of a function, class, or binding definition (let, const, or var). | ||
|
||
export default ["Winter", "Spring", "Summer", "Autumn"]; | ||
|
||
import {days as dayNames} from "date-names"; | ||
|
||
console.log(dayNames.length); | ||
// → 7 |
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,94 @@ | ||
let fifteen = Promise.resolve(15); | ||
fifteen.then(value => console.log(`Got ${value}`)); | ||
// -> Got 15 | ||
|
||
|
||
function storage(nest,name) { | ||
return new Promise(resolve => { | ||
nest.readStorage(name, result => resolve(result)); | ||
}) | ||
} | ||
|
||
storage(bigOak, "enemies") | ||
.then(value => console.log("Got", value)); | ||
|
||
|
||
new Promise((_,reject) => reject(new Error("Fail"))) | ||
.then(value => console.log("Handler 1")) | ||
.catch(reason => { | ||
console.log("Caught failure " + reason); | ||
return "nothing"; | ||
}) | ||
.then(value => console.log("Handler 2", value)); | ||
// -> Caught failure Error: Fail | ||
// -> Handler 2 nothing | ||
|
||
|
||
class Timeout extends Error {} | ||
|
||
function request(nest, target, type, content) { | ||
return new Promise((resolve,reject) => { | ||
let done = false; | ||
function attempt(n){ | ||
nest.send(target, type, content, (failed,value) => { | ||
done = true; | ||
if (failed) reject(failed); | ||
else resolve(value); | ||
}); | ||
setTimeout(() => { | ||
if (done) return; | ||
else if (n < 3) attempt(n + 1); | ||
else reject(new Timeout("Timed out")); | ||
}, 250); | ||
} | ||
attempt(1); | ||
}); | ||
} | ||
|
||
|
||
function requestType(name,handler) { | ||
defineRequestType(name,(nest,content,source,callback) => { | ||
try { | ||
Promise.resolve(handler(nest,content,source)) | ||
.then(response => callback(null,response), | ||
failure => callback(failure)); | ||
} catch (exception) { | ||
callback(exception); | ||
} | ||
}); | ||
} | ||
|
||
|
||
requestType("ping", () => "pong"); | ||
|
||
function availableNeighbors(nest){ | ||
let requests = nest.neighbors.map(neighbor => { | ||
return request(nest, neighbor, "ping") | ||
.then(() => true, () => false); | ||
}); | ||
return Promise.all(requests).then(result => { | ||
return nest.neighbors.filter((_,i) => result[i]); | ||
}); | ||
} | ||
|
||
|
||
|
||
import {everywhere} from "./crow-tech"; | ||
|
||
everywhere(nest => { | ||
nest.state.gossip = []; | ||
}); | ||
|
||
function sendGossip(nest,message,exceptFor = null) { | ||
nest.state.gossip.push(message); | ||
for (let neighbor of nest.neighbors){ | ||
if (neighbor == exceptFor) continue; | ||
request(nest, neighbor, "gossip", message); | ||
} | ||
} | ||
|
||
requestType("gossip",(nest, message, source) => { | ||
if(nest.state.gossip.includes(message)) return; | ||
console.log(`${nest.name} received gossip '${message}' from ${source}`); | ||
sendGossip(nest, message, source); | ||
}); |
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
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,58 @@ | ||
const log = (args) => { | ||
console.log(args); | ||
} | ||
|
||
// Car and cat | ||
let re1 = /^ca(t|r)$/; | ||
log(re1.test("cat")); // -> True | ||
log(re1.test("car")); // -> True | ||
log(re1.test("cas")); // -> False | ||
log(re1.test("cam")); // -> False | ||
log(re1.test("scat")); // -> False | ||
log(re1.test("cats")); // -> False | ||
|
||
// pop and prop | ||
let re2 = /^pr?op$/; | ||
log(re2.test("pop")); // -> True | ||
log(re2.test("prop")); // -> True | ||
log(re2.test("pops")); // -> False | ||
log(re2.test("pnop")); // -> False | ||
|
||
// ferret, ferry, and ferrari | ||
let re3 = /^ferr(et|y|ari)$/ | ||
log(re3.test("ferret")); // -> True | ||
log(re3.test("ferry")); // -> True | ||
log(re3.test("ferrari")); // -> True | ||
log(re3.test("fernet")); // -> False | ||
log(re3.test("ferrart")); // -> False | ||
log(re3.test("ferriri")); // -> False | ||
|
||
|
||
// Any word ending in ious | ||
let re4 = /^[a-z]*ious$/ | ||
log(re4.test("ingenious")); // -> True | ||
log(re4.test("abcious")); // -> True | ||
log(re4.test("ious")); // -> True | ||
log(re4.test("ab1ious")); // -> False | ||
log(re4.test("abcioust")); // -> False | ||
|
||
|
||
// Any whitespace character followed by a period, comma, colon, or semicolon | ||
let re5 = /\s[.|,|:|;]/ | ||
log(re5.test("Hello , how are you?")); // -> True | ||
log(re5.test("Still : what do you think?")); // -> True | ||
log(re5.test("Precious advice, thanks!")); // -> False | ||
|
||
|
||
// A word longer than six letters | ||
let re6 = /\w{7}/ | ||
log(re6.test("abcdefg")); // -> True | ||
log(re6.test("There is abcdefg here")); // -> True | ||
log(re6.test("abcdef")); // -> False | ||
log(re6.test("There is abcdef here")); // -> False | ||
|
||
|
||
// A word without the letter e (or E) | ||
let re7 = /\b[^e\s]+\b/i | ||
log(re7.test("This hasn't the l3tt3r...")); // -> True | ||
log(re7.test("These have the letter")); // -> False |