Skip to content

Commit 8131758

Browse files
committed
load context
1 parent a9cb579 commit 8131758

File tree

10 files changed

+137
-13
lines changed

10 files changed

+137
-13
lines changed

coderoad.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
{
1111
"title": "Filter",
1212
"description": "Array -> Array of items that match a condition",
13-
"explanation": "You've hacked into the school's computer system, and just in time. The grades are in, aren't you're not too proud of your performance. But that's okay, you have a plan: you're going to create a fake report card.\n\nIt would be great if you could `filter` the scores that your parents will see. A `filter` takes a matching condition function and only returns items that result in true. As an example, look at `isB` below:\n\n```\nfunction isB(x) {\n return x === 'b';\n}\n```\n\nLike all of the methods in this chapter, `filter` is already part of the `Array.prototype`, so you can run it after any array. Each item is passed into the params of the condition function, one by one.\n\n```\nvar list = ['a', 'b', 'c'];\nlist.filter(isB);\n//> ['b']\n```\n\nThere's too much student data in the computer system. We'll have to sort through it. Have a look at an example below:\n\n```\nconsole.log(studentData[0]);\n//> { course: 'Web Security',\n// instructor: 'Sue Denim',\n// name: 'Rebecca Heineman',\n// score: 93,\n// grade: 'A' }\n```",
13+
"explanation": "You've hacked into the school's computer system, and just in time. The grades are in, aren't you're not too proud of your performance. But that's okay, you have a plan: you're going to create a fake report card.\n\nIt would be great if you could `filter` the scores that your parents will see. A `filter` takes a matching condition function and only returns items that result in true. As an example, look at `isB` below:\n\n```\nfunction isB(x) {\n return x === 'b';\n}\n```\n\n\nLike all of the methods in this chapter, `filter` is already part of the `Array.prototype`, so you can run it after any array. Each item is passed into the params of the condition function, one by one.\n\n```\nvar list = ['a', 'b', 'c'];\nlist.filter(isB);\n//> ['b']\n```\n\n\nThere's too much student data in the computer system. We'll have to sort through it. Have a look at an example below:\n\n```\nconsole.log(studentData[0]);\n//> { course: 'Web Security',\n// instructor: 'Sue Denim',\n// name: 'Rebecca Heineman',\n// score: 93,\n// grade: 'A' }\n```",
1414
"tasks": [
1515
{
1616
"description": "Write a filter condition function called `isAda` that returns true if param.`name` is true",
@@ -28,7 +28,7 @@
2828
"1/01/02-filter"
2929
],
3030
"actions": [
31-
"insert('var data = require('./tutorial/data/students')(require('./tutorial/data/courses.json')).slice(0);\n')",
31+
"insert('// load data\nvar data = require('./node_modules/coderoad-functional-school/tutorial/data/students').slice(0);\n')",
3232
"insert('var myData = data.filter // call filter condition here\n')"
3333
]
3434
},

tutorial/1/01/01-filter.spec.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
var expect = require('chai').expect;
2-
// loads file context to allow globals
3-
var context = require('test-context');
4-
var filePath = '../../../../../01-filter.js';
5-
context(filePath);
2+
var path = require('path');
3+
var loadContext = require('../../common/loadContext');
4+
var filePath = path.join(process.env.DIR, '01-filter.js');
5+
loadContext(filePath);
6+
67

78
describe('isAda', function() {
89

tutorial/1/01/02-filter.spec.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
var expect = require('chai').expect;
2-
var context = require('test-context');
3-
var filePath = '../../../../../01-filter.js';
4-
context(filePath);
2+
var path = require('path');
3+
var loadContext = require('../../common/loadContext');
4+
var filePath = path.join(process.env.DIR, '01-filter.js');
5+
loadContext(filePath);
56

67
describe('var myData', function() {
78

tutorial/1/01/03-filter.spec.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
var expect = require('chai').expect;
2-
var context = require('test-context');
3-
context('../../../../../01-filter.js');
2+
var path = require('path');
3+
var loadContext = require('../../common/loadContext');
4+
var filePath = path.join(process.env.DIR, '01-filter.js');
5+
loadContext(filePath);
46

57
describe('var myBest', function() {
68

tutorial/1/01/filter.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ function isAda(x) {
4747
@test('1/01/02-filter')
4848
@action(insert(
4949
```
50-
var data = require('./tutorial/data/students')(require('./tutorial/data/courses.json')).slice(0);
50+
// load data
51+
var data = require('./node_modules/coderoad-functional-school/tutorial/lib/students').slice(0);
5152
```))
5253
@action(insert(
5354
```

tutorial/common/loadContext.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var vm = require('vm');
2+
var fs = require('fs');
3+
module.exports = function loadContext(pathToContext) {
4+
var context = fs.readFileSync(pathToContext, 'utf8');
5+
vm.runInThisContext(context);
6+
};

tutorial/data/students.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
var courses = require('./courses.json');
12
require('../common/array');
23

3-
module.exports = function(courses) {
4+
module.exports = getStudentsInCourses();
5+
6+
function getStudentsInCourses() {
47
return courses.map(function(course) {
58
return course.students.map(function(student) {
69
return {

tutorial/data/students2.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var courses = require('./courses2.json');
2+
require('../common/array');
3+
4+
module.exports = getStudentsInCourses();
5+
6+
function getStudentsInCourses() {
7+
return courses.map(function(course) {
8+
return course.students.map(function(student) {
9+
return {
10+
course: course.course,
11+
instructor: course.instructor,
12+
name: student.name,
13+
score: student.score,
14+
grade: student.grade
15+
};
16+
})
17+
}).concatAll();
18+
}

tutorial/lib/students.js

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)