Skip to content

Commit d25653d

Browse files
committed
eslint updates. yarn lock. commonjs support
1 parent b5ca9bd commit d25653d

File tree

7 files changed

+2491
-81
lines changed

7 files changed

+2491
-81
lines changed

.eslintignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
dist/

.eslintrc

-18
This file was deleted.

.eslintrc.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module.exports = {
2+
"extends": ["eslint:recommended", "google"],
3+
"env": {
4+
"browser": true
5+
},
6+
"rules": {
7+
"quotes": [2, "single"],
8+
"prefer-const": 2,
9+
"max-len": [2, 100, {
10+
"ignoreComments": true,
11+
"ignoreUrls": true,
12+
"tabWidth": 2
13+
}],
14+
},
15+
"globals": {
16+
"ga": true,
17+
"window": true
18+
}
19+
}

dist/appmetrics.min.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* Copyright 2016 Copyright (c) 2016 Eric Bidelman. All rights reserved.
2+
* Copyright 2017 Copyright (c) 2016 Eric Bidelman. All rights reserved.
33
44
* @version v0.0.6
55
*
@@ -18,4 +18,5 @@
1818
var _start=new WeakMap,_end=new WeakMap,Metric=function(a){if(!a)throw Error("Please provide a metric name");if(!Metric.supportsPerfMark&&(console.warn("Timeline won't be marked for \""+a+'".'),!Metric.supportsPerfNow))throw Error("This library cannot be used in this browser.");this.name=a};Metric.prototype.log=function(){console.info(this.name,this.duration,"ms");return this};
1919
Metric.prototype.logAll=function(a){a=void 0===a?this.name:a;if(Metric.supportsPerfNow)for(var b=performance.getEntriesByName(a),c=0;c<b.length;++c)console.info(a,b[c].duration,"ms");return this};Metric.prototype.start=function(){if(_start.get(this))return console.warn("Recording already started."),this;_start.set(this,performance.now());Metric.supportsPerfMark&&performance.mark("mark_"+this.name+"_start");return this};
2020
Metric.prototype.end=function(){if(_end.get(this))return console.warn("Recording already stopped."),this;_end.set(this,performance.now());if(Metric.supportsPerfMark){var a="mark_"+this.name+"_start",b="mark_"+this.name+"_end";performance.mark(b);performance.measure(this.name,a,b)}return this};Metric.prototype.sendToAnalytics=function(a,b,c){b=void 0===b?this.name:b;c=void 0===c?this.duration:c;window.ga?0<=c&&ga("send","timing",a,b,c):console.warn("Google Analytics has not been loaded");return this};
21-
Object.defineProperties(Metric.prototype,{duration:{configurable:!0,enumerable:!0,get:function(){var a=_end.get(this)-_start.get(this);if(Metric.supportsPerfMark){var b=performance.getEntriesByName(this.name)[0];b&&"measure"!==b.entryType&&(a=b.duration)}return a||-1}}});Object.defineProperties(Metric,{supportsPerfNow:{configurable:!0,enumerable:!0,get:function(){return self.performance&&performance.now}},supportsPerfMark:{configurable:!0,enumerable:!0,get:function(){return self.performance&&performance.mark}}});
21+
Object.defineProperties(Metric.prototype,{duration:{configurable:!0,enumerable:!0,get:function(){var a=_end.get(this)-_start.get(this);if(Metric.supportsPerfMark){var b=performance.getEntriesByName(this.name)[0];b&&"measure"!==b.entryType&&(a=b.duration)}return a||-1}}});
22+
Object.defineProperties(Metric,{supportsPerfNow:{configurable:!0,enumerable:!0,get:function(){return Boolean(self.performance&&performance.now)}},supportsPerfMark:{configurable:!0,enumerable:!0,get:function(){return Boolean(self.performance&&performance.mark)}}});"undefined"!==typeof module&&(module.exports=Metric);

src/appmetrics.js

+14-27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* eslint-disable no-unused-vars */
2-
31
/**
42
* Copyright 2016 - Eric Bidelman <ebidel@>
53
*
@@ -23,11 +21,11 @@
2321
* API and (optionally) reporting those timings to Google Analytics.
2422
*/
2523

26-
'use strict';
24+
/* eslint-disable no-console */
2725

2826
// Private members.
29-
let _start = new WeakMap();
30-
let _end = new WeakMap();
27+
const _start = new WeakMap();
28+
const _end = new WeakMap();
3129

3230
class Metric {
3331

@@ -37,7 +35,7 @@ class Metric {
3735
* @static
3836
*/
3937
static get supportsPerfNow() {
40-
return self.performance && performance.now;
38+
return Boolean(self.performance && performance.now);
4139
}
4240

4341
/**
@@ -46,7 +44,7 @@ class Metric {
4644
* @static
4745
*/
4846
static get supportsPerfMark() {
49-
return self.performance && performance.mark;
47+
return Boolean(self.performance && performance.mark);
5048
}
5149

5250
/**
@@ -81,27 +79,11 @@ class Metric {
8179

8280
if (!Metric.supportsPerfMark) {
8381
console.warn(`Timeline won't be marked for "${name}".`);
84-
8582
if (!Metric.supportsPerfNow) {
8683
throw Error('This library cannot be used in this browser.');
8784
}
8885
}
8986

90-
// if (window.PerformanceObserver) {}
91-
// let observer = new PerformanceObserver(list => {
92-
// list.getEntries().forEach(entry => {
93-
// // Display each reported measurement on console
94-
// if (console) {
95-
// console.log("Name: " + entry.name +
96-
// ", Type: " + entry.entryType +
97-
// ", Start: " + entry.startTime +
98-
// ", Duration: " + entry.duration + "\n");
99-
// }
100-
// })
101-
// });
102-
// observer.observe({entryTypes: ['resource', 'mark', 'measure']});
103-
// }
104-
10587
this.name = name;
10688
}
10789

@@ -124,9 +106,9 @@ class Metric {
124106
// Use User Timing API results if available, otherwise return
125107
// performance.now() fallback.
126108
if (Metric.supportsPerfNow) {
127-
let items = performance.getEntriesByName(name);
109+
const items = performance.getEntriesByName(name);
128110
for (let i = 0; i < items.length; ++i) {
129-
let item = items[i];
111+
const item = items[i];
130112
console.info(name, item.duration, 'ms');
131113
}
132114
}
@@ -167,8 +149,8 @@ class Metric {
167149

168150
// Support: developer.mozilla.org/en-US/docs/Web/API/Performance/mark
169151
if (Metric.supportsPerfMark) {
170-
let startMark = `mark_${this.name}_start`;
171-
let endMark = `mark_${this.name}_end`;
152+
const startMark = `mark_${this.name}_start`;
153+
const endMark = `mark_${this.name}_end`;
172154
performance.mark(endMark);
173155
performance.measure(this.name, startMark, endMark);
174156
}
@@ -195,3 +177,8 @@ class Metric {
195177
return this;
196178
}
197179
}
180+
181+
if (typeof module !== 'undefined') {
182+
module.exports = Metric;
183+
// export default Metric;
184+
}

test/test.js

+35-34
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,37 @@
11
/* eslint-env node, mocha */
2+
/* global PerformanceObserver, Metric, chai */
23

34
const assert = chai.assert;
45

6+
/* eslint-disable */
7+
function loadAnalytics() {
8+
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
9+
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
10+
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m);
11+
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
12+
ga('create', 'UA-XXXXX-Y', 'auto');
13+
}
14+
/* eslint-enable */
15+
16+
loadAnalytics();
17+
518
describe('appmetrics.js', function() {
619
const METRIC_NAME = 'test_metric';
7-
let metric = new Metric(METRIC_NAME);
8-
9-
function loadAnalytics() {
10-
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
11-
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
12-
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m);
13-
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
14-
ga('create', 'UA-XXXXX-Y', 'auto');
15-
}
20+
const metric = new Metric(METRIC_NAME);
1621

1722
function isAnalyticsRequest(entry) {
1823
return entry.name.includes('/collect') && entry.name.includes('t=timing');
1924
}
2025

2126
if (!window.PerformanceObserver) {
22-
throw 'Cannot run tests in a browser PerformanceObserver';
27+
throw Error('Cannot run tests in a browser PerformanceObserver');
2328
}
2429

2530
before(function() {
2631
if (!location.origin.includes('localhost')) {
2732
assert.fail(false, true, 'Tests need to be run from a web server.');
2833
}
29-
loadAnalytics();
34+
// loadAnalytics();
3035
});
3136

3237
beforeEach(function() {
@@ -44,8 +49,8 @@ describe('appmetrics.js', function() {
4449
assert.equal(metric.duration, -1);
4550
});
4651
it('has correct feature detection', function() {
47-
assert.equal(Metric.supportsPerfNow, performance.now);
48-
assert.equal(Metric.supportsPerfMark, performance.mark);
52+
assert.equal(Metric.supportsPerfNow, true);
53+
assert.equal(Metric.supportsPerfMark, true);
4954
});
5055
it('has private properties', function() {
5156
assert.isUndefined(metric._start);
@@ -54,12 +59,11 @@ describe('appmetrics.js', function() {
5459
});
5560

5661
describe('start()', function() {
57-
5862
it('creates a mark', function(done) {
59-
let observer = new PerformanceObserver(list => {
63+
const observer = new PerformanceObserver(list => {
6064
observer.disconnect();
6165

62-
let entries = list.getEntriesByName(`mark_${METRIC_NAME}_start`);
66+
const entries = list.getEntriesByName(`mark_${METRIC_NAME}_start`);
6367
assert.equal(entries[0].entryType, 'mark', 'not a mark entry');
6468
assert.equal(entries.length, 1);
6569

@@ -79,17 +83,15 @@ describe('appmetrics.js', function() {
7983
});
8084

8185
describe('end()', function() {
82-
8386
it('creates a mark', function(done) {
84-
85-
let observer = new PerformanceObserver(list => {
87+
const observer = new PerformanceObserver(list => {
8688
observer.disconnect();
8789

88-
let markEntries = list.getEntriesByName(`mark_${METRIC_NAME}_end`);
90+
const markEntries = list.getEntriesByName(`mark_${METRIC_NAME}_end`);
8991
assert.equal(markEntries.length, 1);
9092
assert.equal(markEntries[0].entryType, 'mark', 'not a mark entry');
9193

92-
let measureEntries = list.getEntriesByName(METRIC_NAME);
94+
const measureEntries = list.getEntriesByName(METRIC_NAME);
9395
assert.equal(measureEntries[0].entryType, 'measure', 'not a measurement entry');
9496
assert.equal(measureEntries.length, 1);
9597

@@ -125,12 +127,11 @@ describe('appmetrics.js', function() {
125127
});
126128

127129
describe('sendToAnalytics()', function() {
128-
129130
it('sends default request', function(done) {
130-
let observer = new PerformanceObserver(list => {
131+
const observer = new PerformanceObserver(list => {
131132
observer.disconnect();
132133

133-
let entries = list.getEntries().filter(entry => {
134+
const entries = list.getEntries().filter(entry => {
134135
return isAnalyticsRequest(entry) &&
135136
entry.name.includes(metric.duration) &&
136137
entry.name.includes(metric.name) &&
@@ -146,10 +147,10 @@ describe('appmetrics.js', function() {
146147
});
147148

148149
it('can override duration and name', function(done) {
149-
let observer = new PerformanceObserver(list => {
150+
const observer = new PerformanceObserver(list => {
150151
observer.disconnect();
151152

152-
let entries = list.getEntries().filter(entry => {
153+
const entries = list.getEntries().filter(entry => {
153154
return isAnalyticsRequest(entry) &&
154155
entry.name.includes('1234567890') &&
155156
entry.name.includes('category_name') &&
@@ -165,12 +166,12 @@ describe('appmetrics.js', function() {
165166
});
166167

167168
it('can send a duration without measuring', function(done) {
168-
let duration = Date.now();
169+
const duration = Date.now();
169170

170-
let observer = new PerformanceObserver(list => {
171+
const observer = new PerformanceObserver(list => {
171172
observer.disconnect();
172173

173-
let entries = list.getEntries().filter(entry => {
174+
const entries = list.getEntries().filter(entry => {
174175
return isAnalyticsRequest(entry) &&
175176
entry.name.includes(duration) &&
176177
entry.name.includes('category_name') &&
@@ -182,24 +183,24 @@ describe('appmetrics.js', function() {
182183
});
183184
observer.observe({entryTypes: ['resource']});
184185

185-
let metric = new Metric('override_duration');
186+
const metric = new Metric('override_duration');
186187
metric.sendToAnalytics('category_name', metric.name, duration);
187188
});
188189

189190
it('no requests are to GA before a measurement', function(done) {
190191
// If the perf observer sees a request, the test should fail.
191-
let observer = new PerformanceObserver(list => {
192+
const observer = new PerformanceObserver(list => {
192193
observer.disconnect();
193-
assert.fail(false, true, 'Google Analytics request was sent before a measurement was made.');
194+
assert.fail(
195+
false, true, 'Google Analytics request was sent before a measurement was made.');
194196
done();
195197
});
196198
observer.observe({entryTypes: ['resource']});
197199

198-
let metric = new Metric('test_metric');
200+
const metric = new Metric('test_metric');
199201
metric.sendToAnalytics('should_not_be_sent');
200202

201203
setTimeout(done, 500);
202204
});
203205
});
204-
205206
});

0 commit comments

Comments
 (0)