From 0cd2b20b8f76ac3504b77fcc394c58a137ba4784 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Tue, 28 May 2019 23:46:52 +0700 Subject: [PATCH] Require Node.js 8 --- .gitattributes | 3 +-- .travis.yml | 3 ++- browser/create-karma-conf.js | 21 +++++++++++++++------ browser/from-env.js | 2 +- loader.js | 34 +++++++++++++++++----------------- package.json | 16 ++++++++-------- readme.md | 9 +++------ register-shim.js | 10 +++++----- register.js | 32 +++++++++++++------------------- test/browser.js | 28 ++++++++++++---------------- test/defaults.js | 2 +- test/rxjs-observable.js | 2 +- test/rxjs.js | 2 +- test/shortcut-rxjs-all.js | 2 +- test/shortcut-rxjs-min.js | 2 +- test/shortcut-rxjs.js | 2 +- test/shortcut-zen.js | 2 +- test/zen.js | 2 +- 18 files changed, 85 insertions(+), 89 deletions(-) diff --git a/.gitattributes b/.gitattributes index 391f0a4..6313b56 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1 @@ -* text=auto -*.js text eol=lf +* text=auto eol=lf diff --git a/.travis.yml b/.travis.yml index 452a651..1c8da22 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,7 @@ language: node_js node_js: + - '12' + - '10' - '8' - - '6' addons: chrome: stable diff --git a/browser/create-karma-conf.js b/browser/create-karma-conf.js index 5531378..c827917 100644 --- a/browser/create-karma-conf.js +++ b/browser/create-karma-conf.js @@ -1,14 +1,19 @@ 'use strict'; const arrify = require('arrify'); -// Creates a karma.conf implementation that runs a specific set of files. +// Creates a `karma.conf` implementation that runs a specific set of files module.exports = function (files) { - return function (karma) { + return karma => { karma.set({ - frameworks: ['mocha', 'browserify'], + frameworks: [ + 'mocha', + 'browserify' + ], files: arrify(files), preprocessors: { - 'tests/*.js': ['browserify'] + 'tests/*.js': [ + 'browserify' + ] }, browserify: { debug: true @@ -16,10 +21,14 @@ module.exports = function (files) { customLaunchers: { ChromeHeadlessNoSandbox: { base: 'ChromeHeadless', - flags: ['--no-sandbox'] + flags: [ + '--no-sandbox' + ] } }, - browsers: [process.env.CI ? 'ChromeHeadlessNoSandbox' : 'ChromeHeadless'], + browsers: [ + process.env.CI ? 'ChromeHeadlessNoSandbox' : 'ChromeHeadless' + ], autoWatch: false, singleRun: true }); diff --git a/browser/from-env.js b/browser/from-env.js index 26c5e52..cda8a38 100644 --- a/browser/from-env.js +++ b/browser/from-env.js @@ -1,3 +1,3 @@ 'use strict'; -// Karma.conf that reads an environment variable to decide which test to run. +// Karma config that reads an environment variable to decide which test to run module.exports = require('./create-karma-conf')(process.env.ANY_OBSERVABLE_TEST_PATH); diff --git a/loader.js b/loader.js index 7cef2ce..774bd74 100644 --- a/loader.js +++ b/loader.js @@ -1,41 +1,41 @@ 'use strict'; + const REGISTRATION_KEY = Symbol('@@any-observable/REGISTRATION'); -let registered = null; -module.exports = (global, loadImplementation) => { - return (implementation, opts) => { - opts = opts || {}; +let isRegistered; - // global registration unless explicitly {global: false} in options (default true) - const registerGlobal = opts.global !== false; +module.exports = (global, loadImplementation) => { + return (implementation, options = {}) => { + // Global registration unless explicitly `{global: false}`` in options (default true) + const registerGlobal = options.global !== false; // Load any previous global registration - if (registerGlobal && !registered) { - registered = global[REGISTRATION_KEY]; + if (registerGlobal && !isRegistered) { + isRegistered = global[REGISTRATION_KEY]; } - if (registered && implementation && registered.implementation !== implementation) { - throw new Error(`any-observable already defined as "${registered.implementation}". You can only register an implementation before the first call to require('any-observable') and an implementation cannot be changed`); + if (isRegistered && implementation && isRegistered.implementation !== implementation) { + throw new Error(`any-observable already defined as \`${isRegistered.implementation}\`. You can only register an implementation before the first call to \`require('any-observable')\` and an implementation cannot be changed`); } - if (!registered) { + if (!isRegistered) { // Use provided implementation - if (implementation && opts.Observable) { - registered = { - Observable: opts.Observable, + if (implementation && options.Observable) { + isRegistered = { + Observable: options.Observable, implementation }; } else { // Require implementation if implementation is specified but not provided - registered = loadImplementation(implementation || null); + isRegistered = loadImplementation(implementation); } if (registerGlobal) { // Register preference globally in case multiple installations - global[REGISTRATION_KEY] = registered; + global[REGISTRATION_KEY] = isRegistered; } } - return registered; + return isRegistered; }; }; diff --git a/package.json b/package.json index e0135c4..aefcf18 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "url": "sindresorhus.com" }, "engines": { - "node": ">=6" + "node": ">=8" }, "scripts": { "test": "xo && ava" @@ -32,18 +32,18 @@ "rxjs" ], "devDependencies": { - "arrify": "^1.0.1", - "ava": "*", + "arrify": "^2.0.1", + "ava": "^1.4.1", "browserify": "^16.2.0", - "execa": "^0.10.0", - "karma": "^2.0.0", - "karma-browserify": "^5.2.0", + "execa": "^1.0.0", + "karma": "^4.1.0", + "karma-browserify": "^6.0.0", "karma-chrome-launcher": "^2.2.0", "karma-mocha": "^1.3.0", - "mocha": "^5.1.0", + "mocha": "^6.1.4", "rxjs": "^5.5.10", "watchify": "^3.11.0", - "xo": "*", + "xo": "^0.24.0", "zen-observable": "^0.8.8" }, "browser": { diff --git a/readme.md b/readme.md index 1e6be95..ac66f4a 100644 --- a/readme.md +++ b/readme.md @@ -23,7 +23,9 @@ $ npm install zen-observable ```js const Observable = require('any-observable'); // Using `zen-observable` since it's installed -Observable.of(1, 2).forEach(x => console.log(x)); +Observable.of(1, 2).forEach(value => { + console.log(value); +}); //=> 1 //=> 2 ``` @@ -54,8 +56,3 @@ $ ava --require=any-observable/register/zen test.js - [is-observable](https://github.com/sindresorhus/is-observable) - Check if a value is an Observable - [observable-to-promise](https://github.com/sindresorhus/observable-to-promise) - Convert an Observable to a Promise - - -## License - -MIT © [Sindre Sorhus](https://sindresorhus.com) diff --git a/register-shim.js b/register-shim.js index dab3f04..2ca64d2 100644 --- a/register-shim.js +++ b/register-shim.js @@ -3,13 +3,13 @@ module.exports = require('./loader')(window, loadImplementation); /** - * Browser specific loadImplementation. Always uses `window.Observable` - * - * To register a custom implementation, must register with `Observable` option. - */ +Browser specific `loadImplementation`. Always uses `window.Observable`. + +To register a custom implementation, use the `Observable` option. +*/ function loadImplementation() { if (typeof window.Observable === 'undefined') { - throw new TypeError(`any-observable browser requires a polyfill or explicit registration e.g: require('any-observable/register')('rxjs', {Observable: require('rxjs/Observable').Observable})`); + throw new TypeError('any-observable browser requires a polyfill or explicit registration, for example:\nrequire(\'any-observable/register\')(\'rxjs\', {Observable: require(\'rxjs/Observable\').Observable})'); } return { diff --git a/register.js b/register.js index 40b8d86..94a6c49 100644 --- a/register.js +++ b/register.js @@ -2,51 +2,45 @@ module.exports = require('./loader')(global, loadImplementation); function loadImplementation(implementation) { - let impl; + let finalImplementation; if (implementation === 'global.Observable') { // If no implementation or env specified use global.Observable - impl = { + finalImplementation = { Observable: global.Observable, implementation: 'global.Observable' }; } else if (implementation) { // If implementation specified, require it - const lib = require(implementation); + const package_ = require(implementation); - impl = { - Observable: lib.Observable || lib.default || lib, + finalImplementation = { + Observable: package_.Observable || package_.default || package_, implementation }; } else { // Try to auto detect implementation. This is non-deterministic // and should prefer other branches, but this is our last chance - // to load something without throwing error - impl = tryAutoDetect(); + // to load something without throwing error. + finalImplementation = tryAutoDetect(); } - if (!impl) { - throw new Error('Cannot find any-observable implementation nor' + - ' global.Observable. You must install polyfill or call' + - ' require("any-observable/register") with your preferred' + - ' implementation, e.g. require("any-observable/register")(\'rxjs\')' + - ' on application load prior to any require("any-observable").'); + if (!finalImplementation) { + throw new Error('Cannot find any-observable implementation nor `global.Observable`. You must install polyfill or call `require(\'any-observable/register\') with your preferred implementation, for example, `require(\'any-observable/register\')(\'rxjs\')` on app load prior to any `require(\'any-observable\').'); } - return impl; + return finalImplementation; } function tryAutoDetect() { - const libs = [ + const packages = [ 'rxjs', 'zen-observable' ]; - for (const lib of libs) { + for (const package_ of packages) { try { - return loadImplementation(lib); + return loadImplementation(package_); } catch (_) {} } - - return null; } diff --git a/test/browser.js b/test/browser.js index e9c66a1..b2ffbbe 100644 --- a/test/browser.js +++ b/test/browser.js @@ -5,23 +5,19 @@ import execa from 'execa'; const cwd = path.join(__dirname, '..'); const conf = path.join(__dirname, '..', 'browser', 'from-env.js'); -const testsDir = path.join(__dirname, '..', 'browser', 'tests'); +const testsDirectory = path.join(__dirname, '..', 'browser', 'tests'); -const testFiles = fs.readdirSync(testsDir); -const majorVersion = Number(process.version.substring(1).split('.')[0]); +for (const filename of fs.readdirSync(testsDirectory)) { + const basename = path.basename(filename, '.js'); + const filepath = path.join(testsDirectory, filename); -if (majorVersion <= 4) { - test('skipped browser tests for Node.js <= 4', () => {}); -} else { - testFiles.forEach(filename => { - const basename = path.basename(filename, '.js'); - const filepath = path.join(testsDir, filename); - - test.serial(basename, async t => { - await t.notThrows(execa('karma', ['start', conf], { - cwd, - env: Object.assign({}, process.env, {ANY_OBSERVABLE_TEST_PATH: filepath}) - })); - }); + test.serial(basename, async t => { + await t.notThrowsAsync(execa('karma', ['start', conf], { + cwd, + env: { + ...process.env, + ANY_OBSERVABLE_TEST_PATH: filepath + } + })); }); } diff --git a/test/defaults.js b/test/defaults.js index bba8a3f..55ce0a6 100644 --- a/test/defaults.js +++ b/test/defaults.js @@ -3,7 +3,7 @@ const RxJsObservable = require('rxjs').Observable; const AnyObservable = require('..'); const implementation = require('../implementation'); -test(t => { +test('main', t => { t.is(AnyObservable, RxJsObservable); t.is(implementation, 'rxjs'); }); diff --git a/test/rxjs-observable.js b/test/rxjs-observable.js index a67a276..325f6af 100644 --- a/test/rxjs-observable.js +++ b/test/rxjs-observable.js @@ -4,7 +4,7 @@ const RxJsObservable = require('rxjs/Observable').Observable; const AnyObservable = require('..'); const implementation = require('../implementation'); -test(t => { +test('main', t => { t.is(AnyObservable, RxJsObservable); t.is(implementation, 'rxjs/Observable'); }); diff --git a/test/rxjs.js b/test/rxjs.js index 4fa5f37..491375f 100644 --- a/test/rxjs.js +++ b/test/rxjs.js @@ -4,7 +4,7 @@ const RxJsObservable = require('rxjs').Observable; const AnyObservable = require('..'); const implementation = require('../implementation'); -test(t => { +test('main', t => { t.is(AnyObservable, RxJsObservable); t.is(implementation, 'rxjs'); }); diff --git a/test/shortcut-rxjs-all.js b/test/shortcut-rxjs-all.js index 75b6da7..0c48a23 100644 --- a/test/shortcut-rxjs-all.js +++ b/test/shortcut-rxjs-all.js @@ -4,7 +4,7 @@ import {Observable as RxJsObservable} from 'rxjs'; import AnyObservable from '..'; import implementation from '../implementation'; -test(t => { +test('main', t => { t.is(AnyObservable, RxJsObservable); t.is(implementation, 'rxjs'); t.is(typeof RxJsObservable.prototype.map, 'function'); diff --git a/test/shortcut-rxjs-min.js b/test/shortcut-rxjs-min.js index 2fcc363..8ba26a2 100644 --- a/test/shortcut-rxjs-min.js +++ b/test/shortcut-rxjs-min.js @@ -4,7 +4,7 @@ import {Observable as RxJsObservable} from 'rxjs/Observable'; import AnyObservable from '..'; import implementation from '../implementation'; -test(t => { +test('main', t => { t.is(AnyObservable, RxJsObservable); t.is(implementation, 'rxjs/Observable'); t.is(typeof RxJsObservable.of, 'undefined'); diff --git a/test/shortcut-rxjs.js b/test/shortcut-rxjs.js index 5963f19..fc0d83f 100644 --- a/test/shortcut-rxjs.js +++ b/test/shortcut-rxjs.js @@ -4,7 +4,7 @@ import {Observable as RxJsObservable} from 'rxjs/Observable'; import AnyObservable from '..'; import implementation from '../implementation'; -test(t => { +test('main', t => { t.is(AnyObservable, RxJsObservable); t.is(implementation, 'rxjs/Observable'); t.is(typeof RxJsObservable.of, 'function'); diff --git a/test/shortcut-zen.js b/test/shortcut-zen.js index 26f79c0..c608d4b 100644 --- a/test/shortcut-zen.js +++ b/test/shortcut-zen.js @@ -4,7 +4,7 @@ import ZenObservable from 'zen-observable'; import AnyObservable from '..'; import implementation from '../implementation'; -test(t => { +test('main', t => { t.is(AnyObservable, ZenObservable); t.is(implementation, 'zen-observable'); }); diff --git a/test/zen.js b/test/zen.js index 9a7133b..e19b9ea 100644 --- a/test/zen.js +++ b/test/zen.js @@ -4,7 +4,7 @@ const ZenObservable = require('zen-observable'); const AnyObservable = require('..'); const implementation = require('../implementation'); -test(t => { +test('main', t => { t.is(AnyObservable, ZenObservable); t.is(implementation, 'zen-observable'); });