Skip to content

Commit 7726b7d

Browse files
committed
rename polyfills -> utils. Improve utils documentation
1 parent 0c7b687 commit 7726b7d

File tree

17 files changed

+113
-8
lines changed

17 files changed

+113
-8
lines changed

lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ var ReactDOM = require('react-dom');
33
var SidePanel_1 = require('./components/SidePanel');
44
var StatusBar_1 = require('./components/StatusBar');
55
var setup_1 = require('./modules/setup');
6-
var polyfills_1 = require('./polyfills');
76
var store_1 = require('./store');
87
var subscriptions_1 = require('./subscriptions');
8+
var utils_1 = require('./utils');
99
var injectTapEventPlugin = require('react-tap-event-plugin');
1010
process.env.NODE_ENV = 'production';
1111
var Main = (function () {
1212
function Main() {
1313
injectTapEventPlugin();
14-
polyfills_1.default();
14+
utils_1.default();
1515
store_1.default.dispatch(setup_1.setupVerify());
1616
this.side = SidePanel_1.sideElement.init();
1717
this.subscriptions = new subscriptions_1.default();

lib/modules/tests/test-run/config-path.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22
var path_1 = require('path');
3-
var system_1 = require('../../../polyfills/system');
3+
var system_1 = require('../../../utils/system');
44
function configPath(_a) {
55
var dir = _a.dir, tutorial = _a.tutorial, testPath = _a.testPath;
66
if (system_1.isWindows) {

lib/modules/tutorial/utils/config-runner.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22
var path_1 = require('path');
3-
var system_1 = require('../../../polyfills/system');
3+
var system_1 = require('../../../utils/system');
44
var node_file_exists_1 = require('node-file-exists');
55
function configRunner(name, runner, dir) {
66
var flatDep = path_1.join(dir, 'node_modules', runner, 'package.json');
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
var fetch_1 = require('../../utils/fetch');
3+
var apiCall = function (name) { return ("https://registry.npmjs.org/" + name); };
4+
function getLatestVersion(name, current) {
5+
return fetch_1.default(apiCall(name))
6+
.then(function (res) {
7+
if (res) {
8+
JSON.parse(res)['dist-tags'].latest;
9+
return true;
10+
}
11+
return false;
12+
});
13+
}

lib/utils/fetch.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"use strict";
2+
var fetch = function (url) {
3+
return new Promise(function (resolve, reject) {
4+
var lib = url.startsWith('https') ? require('https') : require('http');
5+
var request = lib.get(url, function (response) {
6+
if (response.statusCode < 200 || response.statusCode > 299) {
7+
reject(new Error('Failed to load page, status code: ' + response.statusCode));
8+
}
9+
var body = [];
10+
response.on('data', function (chunk) { return body.push(chunk); });
11+
response.on('end', function () { return resolve(body.join('')); });
12+
});
13+
request.on('error', function (err) { return reject(err); });
14+
});
15+
};
16+
Object.defineProperty(exports, "__esModule", { value: true });
17+
exports.default = fetch;

lib/utils/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
var object_values_1 = require('./object-values');
3+
function loadPolyfills() {
4+
object_values_1.default();
5+
}
6+
Object.defineProperty(exports, "__esModule", { value: true });
7+
exports.default = loadPolyfills;

lib/utils/object-values.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"use strict";
2+
function polyfillObjectValues() {
3+
if (typeof Object.values !== 'function') {
4+
Object.values = function (obj) {
5+
var vals = new Set();
6+
for (var key in obj) {
7+
vals.add(obj[key]);
8+
}
9+
return Array.from(vals);
10+
};
11+
}
12+
}
13+
Object.defineProperty(exports, "__esModule", { value: true });
14+
exports.default = polyfillObjectValues;

lib/utils/system.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"use strict";
2+
exports.isWindows = window.navigator.appVersion.indexOf('Win') > -1 || false;

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import * as ReactDOM from 'react-dom';
44
import {SideRoot, sideElement} from './components/SidePanel';
55
import addToStatusBar from './components/StatusBar';
66
import {setupVerify} from './modules/setup';
7-
import loadPolyfills from './polyfills';
87
import store from './store';
98
import Subscriptions from './subscriptions';
9+
import loadPolyfills from './utils/polyfills';
1010
import * as injectTapEventPlugin from 'react-tap-event-plugin';
1111

1212
// React optimization

src/modules/tests/test-run/config-path.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {join} from 'path';
22

3-
import {isWindows} from '../../../polyfills/system';
3+
import {isWindows} from '../../../utils/system';
44

55
/**
66
* set paths to tests as absolute paths

src/modules/tutorial/utils/config-runner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {join} from 'path';
2-
import {isWindows} from '../../../polyfills/system';
2+
import {isWindows} from '../../../utils/system';
33
import fileExists from 'node-file-exists';
44

55
/**

src/modules/tutorial/utils/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {join} from 'path';
22

33
import {configIssuesPath, configRepo} from './config-repo';
44
import configRunner from './config-runner';
5-
import {isWindows} from '../../../polyfills/system';
5+
import {isWindows} from '../../../utils/system';
66
import fileExists from 'node-file-exists';
77

88
/**
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import fetch from '../../utils/fetch';
2+
3+
const apiCall = name => `https://registry.npmjs.org/${name}`;
4+
5+
function getLatestVersion(name: string, current: string): Promise<boolean> {
6+
return fetch(apiCall(name))
7+
.then((res: string) => {
8+
if (res) {
9+
JSON.parse(res)['dist-tags'].latest;
10+
return true;
11+
}
12+
return false;
13+
});
14+
}

src/utils/fetch.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* fetch data from url
3+
* @param {string} url
4+
* @returns Promise
5+
*/
6+
const fetch = function fetch (url: string): Promise<Object> {
7+
return new Promise((resolve, reject) => {
8+
const lib = url.startsWith('https') ? require('https') : require('http');
9+
const request = lib.get(url, (response) => {
10+
// handle http errors
11+
if (response.statusCode < 200 || response.statusCode > 299) {
12+
reject(new Error('Failed to load page, status code: ' + response.statusCode));
13+
}
14+
const body = [];
15+
// on every content chunk, push it to the data array
16+
response.on('data', (chunk) => body.push(chunk));
17+
// we are done, resolve promise with those joined chunks
18+
response.on('end', () => resolve(body.join('')));
19+
});
20+
// handle connection errors of the request
21+
request.on('error', (err) => reject(err))
22+
})
23+
};
24+
25+
export default fetch;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import polyfillObjectValues from './object-values';
22

3+
/**
4+
* calls list of polyfills
5+
* @returns void
6+
*/
37
export default function loadPolyfills(): void {
48
polyfillObjectValues();
59
}

src/polyfills/object-values.ts renamed to src/utils/polyfills/object-values.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* Object.values polyfill
3+
* https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Object/values
4+
* @returns void
5+
*/
16
export default function polyfillObjectValues(): void {
27
// Object.values (ES7)
38
if (typeof Object.values !== 'function') {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1+
/**
2+
* checks if system is Windows
3+
* @returns boolean
4+
*/
15
export const isWindows = window.navigator.appVersion.indexOf('Win') > -1 || false;

0 commit comments

Comments
 (0)