Skip to content
This repository was archived by the owner on Mar 31, 2025. It is now read-only.

Commit bdbb7c2

Browse files
refact(examples/exampleMap): rename and convert to a StringMap
1 parent 42629b4 commit bdbb7c2

File tree

11 files changed

+59
-48
lines changed

11 files changed

+59
-48
lines changed

examples/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = new Package('examples', ['jsdoc'])
88
.processor(require('./processors/examples-generate'))
99
.processor(require('./processors/protractor-generate'))
1010

11-
.factory(require('./services/examples'))
11+
.factory(require('./services/exampleMap'))
1212
.factory(require('./inline-tag-defs/runnableExample'))
1313

1414
.config(function(templateFinder, generateExamplesProcessor) {

examples/inline-tag-defs/runnableExample.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
* @description
44
* Inject the specified runnable example into the doc
55
*/
6-
module.exports = function runnableExampleInlineTagDef(examples, createDocMessage) {
6+
module.exports = function runnableExampleInlineTagDef(exampleMap, createDocMessage) {
77
return {
88
name: 'runnableExample',
99

1010
handler: function(doc, tagName, description) {
1111

1212
// The tag description should contain the id of the runnable example doc
13-
var example = examples[description];
13+
var example = exampleMap.get(description);
1414
if ( !example ) {
1515
throw new Error(createDocMessage('No example exists with id "' + description + '".', doc));
1616
}

examples/processors/examples-generate.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var path = require('canonical-path');
88
* This includes the files that will be run in an iframe, the code that will be injected
99
* into the HTML pages and the protractor test files.
1010
*/
11-
module.exports = function generateExamplesProcessor(log, examples) {
11+
module.exports = function generateExamplesProcessor(log, exampleMap) {
1212

1313
return {
1414
$runAfter: ['adding-extra-docs'],
@@ -17,7 +17,8 @@ module.exports = function generateExamplesProcessor(log, examples) {
1717
deployments: { presence: true }
1818
},
1919
$process: function(docs) {
20-
_.forEach(examples, function(example) {
20+
var that = this;
21+
exampleMap.forEach(function(example) {
2122

2223
var stylesheets = [];
2324
var scripts = [];
@@ -29,7 +30,7 @@ module.exports = function generateExamplesProcessor(log, examples) {
2930
_.forEach(example.files, function(file, fileName) {
3031
if ( fileName === 'index.html' ) return;
3132

32-
var fileDoc = this.createFileDoc(example, file);
33+
var fileDoc = that.createFileDoc(example, file);
3334
docs.push(fileDoc);
3435

3536
// Store a reference to the fileDoc for attaching to the exampleDocs
@@ -38,24 +39,24 @@ module.exports = function generateExamplesProcessor(log, examples) {
3839
} else if ( file.type == 'js' ) {
3940
scripts.push(fileDoc);
4041
}
41-
}, this);
42+
});
4243

4344
// Create an index.html document for the example (one for each deployment type)
44-
_.forEach(this.deployments, function(deployment) {
45-
var exampleDoc = this.createExampleDoc(example, deployment, stylesheets, scripts);
45+
_.forEach(that.deployments, function(deployment) {
46+
var exampleDoc = that.createExampleDoc(example, deployment, stylesheets, scripts);
4647
docs.push(exampleDoc);
4748
example.deployments[deployment.name] = exampleDoc;
48-
}, this);
49+
});
4950

5051
// Create the doc that will be injected into the website as a runnable example
51-
var runnableExampleDoc = this.createRunnableExampleDoc(example);
52+
var runnableExampleDoc = that.createRunnableExampleDoc(example);
5253
docs.push(runnableExampleDoc);
5354
example.runnableExampleDoc = runnableExampleDoc;
5455

5556
// Create the manifest that will be sent to Plunker
56-
docs.push(this.createManifestDoc(example));
57+
docs.push(that.createManifestDoc(example));
5758

58-
}, this);
59+
});
5960
},
6061

6162
createExampleDoc: function(example, deployment, stylesheets, scripts) {

examples/processors/examples-parse.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var FILE_REGEX = /<file([^>]*)>([\S\s]+?)<\/file>/g;
1111
* @description
1212
* Search the documentation for examples that need to be extracted
1313
*/
14-
module.exports = function parseExamplesProcessor(log, examples, trimIndentation, createDocMessage) {
14+
module.exports = function parseExamplesProcessor(log, exampleMap, trimIndentation, createDocMessage) {
1515
return {
1616
$runAfter: ['files-read'],
1717
$runBefore: ['parsing-tags'],
@@ -22,7 +22,7 @@ module.exports = function parseExamplesProcessor(log, examples, trimIndentation,
2222
doc.content = doc.content.replace(EXAMPLE_REGEX, function processExample(match, attributeText, exampleText) {
2323

2424
var example = extractAttributes(attributeText);
25-
var id = uniqueName(examples, 'example-' + (example.name || 'example'));
25+
var id = uniqueName(exampleMap, 'example-' + (example.name || 'example'));
2626
_.assign(example, {
2727
attributes: _.omit(example, ['files', 'doc']),
2828
files: extractFiles(exampleText),
@@ -33,7 +33,7 @@ module.exports = function parseExamplesProcessor(log, examples, trimIndentation,
3333

3434
// store the example information for later
3535
log.debug('Storing example', id);
36-
examples[id] = example;
36+
exampleMap.set(id, example);
3737

3838
return '{@runnableExample ' + id + '}';
3939
});
@@ -46,7 +46,7 @@ module.exports = function parseExamplesProcessor(log, examples, trimIndentation,
4646
};
4747

4848
function extractAttributes(attributeText) {
49-
var attributes = Object.create(null);
49+
var attributes = {};
5050
attributeText.replace(ATTRIBUTE_REGEX, function(match, prop, val1, val2){
5151
attributes[prop] = val1 || val2;
5252
});
@@ -73,10 +73,10 @@ module.exports = function parseExamplesProcessor(log, examples, trimIndentation,
7373
return files;
7474
}
7575

76-
function uniqueName(container, name) {
77-
if ( container[name] ) {
76+
function uniqueName(containerMap, name) {
77+
if ( containerMap.has(name) ) {
7878
var index = 1;
79-
while(container[name + index]) {
79+
while(containerMap.has(name + index)) {
8080
index += 1;
8181
}
8282
name = name + index;

examples/processors/protractor-generate.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var path = require('canonical-path');
88
* @description
99
* Generate a protractor test files from the e2e tests in the examples
1010
*/
11-
module.exports = function generateProtractorTestsProcessor(examples) {
11+
module.exports = function generateProtractorTestsProcessor(exampleMap) {
1212
return {
1313
deployments: [],
1414
$validate: {
@@ -20,7 +20,7 @@ module.exports = function generateProtractorTestsProcessor(examples) {
2020

2121
var deployments = this.deployments;
2222

23-
_.forEach(examples, function(example) {
23+
exampleMap.forEach(function(example) {
2424
_.forEach(example.files, function(file) {
2525

2626
// Check if it's a Protractor test.

examples/services/exampleMap.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var StringMap = require('stringmap');
2+
3+
/**
4+
* @dgService exampleMap
5+
* @description
6+
* A map of examples parsed out of the doc content, keyed on
7+
*/
8+
module.exports = function exampleMap() {
9+
return new StringMap();
10+
};

examples/services/examples.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

examples/spec/index.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ describe('examples package', function() {
7474

7575
it("should compute the path of examples from their attributes", function(done) {
7676
processExample().then(function(docs) {
77+
7778
expect(docs.length).toEqual(4);
7879

7980
expect(docs[0].id).toEqual('example-testExample/app.js');

examples/spec/inline-tag-defs/runnableExample.spec.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
var tagDefinitionFactory = require('../../inline-tag-defs/runnableExample');
22
var createDocMessageFactory = require('../../../base/services/createDocMessage');
3+
var StringMap = require('stringmap');
34

45
describe("runnableExampleInlineTagDef", function() {
56

6-
var examples, tagDef;
7+
var exampleMap, tagDef;
78

89
beforeEach(function() {
9-
examples = {};
10-
examples['some-example'] = {
10+
exampleMap = new StringMap();
11+
exampleMap.set('some-example', {
1112
runnableExampleDoc: {
1213
renderedContent: 'The rendered content of the some-example example'
1314
}
14-
};
15-
tagDef = tagDefinitionFactory(examples, createDocMessageFactory());
15+
});
16+
tagDef = tagDefinitionFactory(exampleMap, createDocMessageFactory());
1617
});
1718

1819
it("should have the correct name", function() {

examples/spec/processors/examples-generate.spec.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
var generateExamplesProcessorFactory = require('../../processors/examples-generate');
22
var mockLog = require('dgeni/lib/mocks/log');
33
var _ = require('lodash');
4+
var StringMap = require('stringmap');
45

56
describe("examples-generate processor", function() {
6-
var templateFolder, deployments, docs, examples;
7+
var templateFolder, deployments, docs, exampleMap;
78

89
beforeEach(function() {
910

1011
docs = [{ file: 'a.b.js' }];
1112

12-
examples = {};
13+
exampleMap = new StringMap();
1314

1415
files = {};
1516

@@ -18,16 +19,16 @@ describe("examples-generate processor", function() {
1819
files['app.css'] = { type: 'css', name: 'app.css', fileContents: 'app.css content' };
1920
files['app.spec.js'] = { type: 'spec', name: 'app.spec.js', fileContents: 'app.spec.js content' };
2021

21-
examples['a.b.c'] = {
22+
exampleMap.set('a.b.c', {
2223
id: 'a.b.c',
2324
doc: docs[0],
2425
outputFolder: 'examples',
2526
deps: 'dep1.js;dep2.js',
2627
files: files,
2728
deployments: {}
28-
};
29+
});
2930

30-
processor = generateExamplesProcessorFactory(mockLog, examples);
31+
processor = generateExamplesProcessorFactory(mockLog, exampleMap);
3132
processor.templateFolder = 'examples';
3233
processor.deployments = [
3334
{
@@ -95,6 +96,6 @@ describe("examples-generate processor", function() {
9596
var manifestDoc = _.filter(docs, { docType: 'example-file', template: 'manifest.template.json' })[0];
9697
expect(manifestDoc.id).toEqual('a.b.c/manifest.json');
9798
expect(manifestDoc.docType).toEqual('example-file');
98-
expect(manifestDoc.example).toEqual(examples['a.b.c']);
99+
expect(manifestDoc.example).toEqual(exampleMap.get('a.b.c'));
99100
});
100101
});

examples/spec/processors/examples-parse.spec.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ var parseExamplesProcessorFactory = require('../../processors/examples-parse');
22
var mockLog = require('dgeni/lib/mocks/log')(false);
33
var createDocMessageFactory = require('../../../base/services/createDocMessage');
44
var _ = require('lodash');
5+
var StringMap = require('stringmap');
56

6-
describe("examples-parse doc processor", function() {
7+
describe("parseExamplesProcessor", function() {
78

8-
var processor, examples, mockTrimIndentation;
9+
var processor, exampleMap, mockTrimIndentation;
910

1011
beforeEach(function() {
11-
examples = {};
12+
exampleMap = new StringMap();
1213
mockTrimIndentation = jasmine.createSpy('trimIndentation').and.callFake(function(value) { return value; });
13-
processor = parseExamplesProcessorFactory(mockLog, examples, mockTrimIndentation, createDocMessageFactory());
14+
processor = parseExamplesProcessorFactory(mockLog, exampleMap, mockTrimIndentation, createDocMessageFactory());
1415
});
1516

1617
it("should extract example tags from the doc content", function() {
@@ -27,12 +28,12 @@ describe("examples-parse doc processor", function() {
2728
}
2829
];
2930
processor.$process(docs);
30-
expect(examples['example-bar']).toEqual(jasmine.objectContaining({ name:'bar', moo1:'nar1', id: 'example-bar'}));
31-
expect(examples['example-bar1']).toEqual(jasmine.objectContaining({ name:'bar', moo2:'nar2', id: 'example-bar1'}));
32-
expect(examples['example-value']).toEqual(jasmine.objectContaining({ name:'value', id: 'example-value'}));
33-
expect(examples['example-with-files']).toEqual(jasmine.objectContaining({ name: 'with-files', id: 'example-with-files'}));
31+
expect(exampleMap.get('example-bar')).toEqual(jasmine.objectContaining({ name:'bar', moo1:'nar1', id: 'example-bar'}));
32+
expect(exampleMap.get('example-bar1')).toEqual(jasmine.objectContaining({ name:'bar', moo2:'nar2', id: 'example-bar1'}));
33+
expect(exampleMap.get('example-value')).toEqual(jasmine.objectContaining({ name:'value', id: 'example-value'}));
34+
expect(exampleMap.get('example-with-files')).toEqual(jasmine.objectContaining({ name: 'with-files', id: 'example-with-files'}));
3435

35-
var files = examples['example-with-files'].files;
36+
var files = exampleMap.get('example-with-files').files;
3637
var file = files['app.js'];
3738
expect(file.name).toEqual('app.js');
3839
expect(file.type).toEqual('js');
@@ -53,8 +54,8 @@ describe("examples-parse doc processor", function() {
5354
'<example name="bar">some example content 2</example>'
5455
}];
5556
processor.$process(docs);
56-
expect(examples['example-bar'].id).toEqual('example-bar');
57-
expect(examples['example-bar1'].id).toEqual('example-bar1');
57+
expect(exampleMap.get('example-bar').id).toEqual('example-bar');
58+
expect(exampleMap.get('example-bar1').id).toEqual('example-bar1');
5859
});
5960

6061
it("should inject a new set of elements in place of the example into the original markup to be used by the template", function() {

0 commit comments

Comments
 (0)