Skip to content

Commit

Permalink
Translated CommonJS to ES Modules; waiting on standard-things/esm#706
Browse files Browse the repository at this point in the history
  • Loading branch information
HughxDev committed Apr 27, 2019
1 parent 64c1a6b commit fd0d55f
Show file tree
Hide file tree
Showing 20 changed files with 465 additions and 117 deletions.
87 changes: 44 additions & 43 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,52 @@
module.exports = {
"plugins": ["lodash"],
// "plugins": ["lodash"],
"extends": [
"eslint-config-hughx",
// "plugin:lodash/recommended",
],
"rules": {
"lodash/callback-binding": 2,
"lodash/chain-style": [2, "as-needed"],
"lodash/chaining": 2,
"lodash/collection-method-value": 2,
"lodash/collection-ordering": 2,
"lodash/collection-return": 2,
"lodash/consistent-compose": [2, "flow"],
"lodash/identity-shorthand": [2, "always"],
"lodash/import-scope": [2],
"lodash/matches-prop-shorthand": [2, "always"],
"lodash/matches-shorthand": [2, "always", 3],
"lodash/no-commit": 2,
"lodash/no-double-unwrap": 2,
"lodash/no-extra-args": 2,
"lodash/no-unbound-this": 2,
"lodash/path-style": [2, "string"],
"lodash/prefer-compact": 2,
// "lodash/prefer-constant": 2,
"lodash/prefer-filter": [2, 3],
"lodash/prefer-find": 2,
"lodash/prefer-flat-map": 2,
"lodash/prefer-get": [2, 3],
"lodash/prefer-immutable-method": 2,
// "lodash/prefer-includes": [2, {includeNative: true}],
"lodash/prefer-invoke-map": 2,
"lodash/prefer-is-nil": 2,
"lodash/prefer-lodash-chain": 2,
// "lodash/prefer-lodash-method": 2,
"lodash/prefer-lodash-typecheck": 2,
"lodash/prefer-map": 2,
"lodash/prefer-matches": [2, 3],
"lodash/prefer-noop": 2,
"lodash/prefer-over-quantifier": 2,
"lodash/prefer-reject": [2, 3],
"lodash/prefer-some": [2, {includeNative: true}],
"lodash/prefer-startswith": 2,
"lodash/prefer-thru": 2,
"lodash/prefer-times": 2,
"lodash/prefer-wrapper-method": 2,
"lodash/preferred-alias": 2,
"lodash/prop-shorthand": [2, "always"],
"lodash/unwrap": 2,
// "lodash/callback-binding": 2,
// "lodash/chain-style": [2, "as-needed"],
// "lodash/chaining": 2,
// "lodash/collection-method-value": 2,
// "lodash/collection-ordering": 2,
// "lodash/collection-return": 2,
// "lodash/consistent-compose": [2, "flow"],
// "lodash/identity-shorthand": [2, "always"],
// "lodash/import-scope": [2],
// "lodash/matches-prop-shorthand": [2, "always"],
// "lodash/matches-shorthand": [2, "always", 3],
// "lodash/no-commit": 2,
// "lodash/no-double-unwrap": 2,
// "lodash/no-extra-args": 2,
// "lodash/no-unbound-this": 2,
// "lodash/path-style": [2, "string"],
// "lodash/prefer-compact": 2,
// // "lodash/prefer-constant": 2,
// "lodash/prefer-filter": [2, 3],
// "lodash/prefer-find": 2,
// "lodash/prefer-flat-map": 2,
// "lodash/prefer-get": [2, 3],
// "lodash/prefer-immutable-method": 2,
// // "lodash/prefer-includes": [2, {includeNative: true}],
// "lodash/prefer-invoke-map": 2,
// "lodash/prefer-is-nil": 2,
// "lodash/prefer-lodash-chain": 2,
// // "lodash/prefer-lodash-method": 2,
// "lodash/prefer-lodash-typecheck": 2,
// "lodash/prefer-map": 2,
// "lodash/prefer-matches": [2, 3],
// "lodash/prefer-noop": 2,
// "lodash/prefer-over-quantifier": 2,
// "lodash/prefer-reject": [2, 3],
// "lodash/prefer-some": [2, {includeNative: true}],
// "lodash/prefer-startswith": 2,
// "lodash/prefer-thru": 2,
// "lodash/prefer-times": 2,
// "lodash/prefer-wrapper-method": 2,
// "lodash/preferred-alias": 2,
// "lodash/prop-shorthand": [2, "always"],
// "lodash/unwrap": 2,
"import/extensions": "ignorePackages",
},
};
3 changes: 3 additions & 0 deletions index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Set options as a parameter, environment variable, or rc file.
require = require( 'esm' )( module ); // eslint-disable-line no-global-assign
module.exports = require( './index.js' );
14 changes: 7 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const fs = require( 'fs' );
const xml = require( 'libxmljs' );
const set = require( 'lodash/set' );
const { exec } = require( 'child_process' );
import fs from 'fs';
import xml from 'libxmljs';
import set from 'lodash/set';
import { exec } from 'child_process';

const Video = require( './video' );
const Validation = require( './util/validation' );
import Video from './video';
import Validation from './util/validation';

class HVML {
constructor( path, config = {} ) {
Expand Down Expand Up @@ -524,4 +524,4 @@ class HVML {

// toJson

module.exports = { HVML, Video };
export { HVML, Video };
2 changes: 1 addition & 1 deletion index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const skipIf = require( 'skip-if' );
const { execSync } = require( 'child_process' );

const { HVML } = require( './index.js' );
const { HVML } = require( './index.cjs' );

// test( 'opens files successfully', () => {} );

Expand Down
5 changes: 4 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
"collectCoverage": true,
"collectCoverage": false,
"coverageReporters": [
"json-summary",
"text",
Expand All @@ -12,4 +12,7 @@ module.exports = {
".eslintrc.js",
"jest.config.js",
],
// "transform": {
// "^.+\\.js$": "esm",
// },
};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
},
"homepage": "https://github.com/RedBlueVideo/hvml#readme",
"dependencies": {
"esm": "^3.2.22",
"libxmljs": "^0.19.5",
"lodash": "^4.17.11",
"md2jsonml": "^2.0.0",
Expand All @@ -47,6 +48,7 @@
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-lodash": "^5.1.0",
"jest": "^24.1.0",
"nyc": "^14.0.0",
"recursive-copy": "^2.0.10",
"rimraf": "^2.6.3",
"skip-if": "^1.1.1"
Expand Down
2 changes: 1 addition & 1 deletion util/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function ucFirst( string ) {
return `${string.charAt( 0 ).toUpperCase()}${string.slice( 1 )}`;
}

module.exports = {
export {
softTrim,
ucFirst,
};
3 changes: 3 additions & 0 deletions util/time.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Set options as a parameter, environment variable, or rc file.
require = require( 'esm' )( module ); // eslint-disable-line no-global-assign
module.exports = require( './time.js' );
2 changes: 1 addition & 1 deletion util/time.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,4 @@ class Time {
}
}

module.exports = Time;
export default Time;
2 changes: 1 addition & 1 deletion util/time.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Time = require( './time' );
const Time = require( './time.cjs' ).default;

describe( 'Time', () => {
it( 'parses ISO 8601 durations', () => {
Expand Down
3 changes: 3 additions & 0 deletions util/transform.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Set options as a parameter, environment variable, or rc file.
require = require( 'esm' )( module ); // eslint-disable-line no-global-assign
module.exports = require( './transform.js' );
12 changes: 6 additions & 6 deletions util/transform.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const md2jsonml = require( 'md2jsonml' );
const xmlTrident = require( 'xml-trident' );
const isString = require( 'lodash/isString' );
const isPlainObject = require( 'lodash/isPlainObject' );
import md2jsonml from 'md2jsonml';
import * as xmlTrident from 'xml-trident';
import isString from 'lodash/isString';
import isPlainObject from 'lodash/isPlainObject';

const { softTrim } = require( './strings' );
import { softTrim } from './strings';

class Transform {
static markdownToJsonMl( input ) {
Expand Down Expand Up @@ -76,4 +76,4 @@ class Transform {
}
}

module.exports = Transform;
export default Transform;
2 changes: 1 addition & 1 deletion util/transform.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Transform = require( './transform' );
const Transform = require( './transform.cjs' ).default;

describe( 'Transform', () => {
test( 'markdownToJsonMl', () => {
Expand Down
3 changes: 3 additions & 0 deletions util/validation.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Set options as a parameter, environment variable, or rc file.
require = require( 'esm' )( module ); // eslint-disable-line no-global-assign
module.exports = require( './validation.js' );
78 changes: 39 additions & 39 deletions util/validation.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { ucFirst } = require( './strings' );
import { ucFirst } from './strings.js';

// https://rclayton.silvrback.com/custom-errors-in-node-js
class HVMLDomainError extends Error {
Expand All @@ -13,43 +13,6 @@ class HVMLDomainError extends Error {
}
}

class HVMLEnumError extends HVMLDomainError {
/* className, field, badValues */
constructor( data ) {
let forField = '';

if ( !data ) {
const error = new HVMLTypeError( {
"className": "HVMLEnumError",
"expected": "Object",
"input": data,
"methodName": "constructor",
"fieldName": "data",
} );

throw new TypeError( error.message );
} else if ( !( 'badValues' in data ) ) {
const error = new HVMLTypeError( {
"className": "HVMLEnumError",
"expected": "Array",
"input": data.badValues,
"methodName": "constructor",
"fieldName": "data.badValues",
} );

throw new TypeError( error.message );
}

if ( 'className' in data ) {
forField += ` for ${data.className}${data.fieldName ? ( '::' + data.fieldName ) : '.constructor'}`; // eslint-disable-line prefer-template
}

super( `The following values are invalid${forField}: ${data.badValues.join( ', ' )}` );

this.data = data;
}
}

class HVMLTypeError extends HVMLDomainError {
/* className, field, expected, extraInfo = '' */
static getGot( data ) {
Expand Down Expand Up @@ -152,6 +115,43 @@ class HVMLTypeError extends HVMLDomainError {
}
}

class HVMLEnumError extends HVMLDomainError {
/* className, field, badValues */
constructor( data ) {
let forField = '';

if ( !data ) {
const error = new HVMLTypeError( {
"className": "HVMLEnumError",
"expected": "Object",
"input": data,
"methodName": "constructor",
"fieldName": "data",
} );

throw new TypeError( error.message );
} else if ( !( 'badValues' in data ) ) {
const error = new HVMLTypeError( {
"className": "HVMLEnumError",
"expected": "Array",
"input": data.badValues,
"methodName": "constructor",
"fieldName": "data.badValues",
} );

throw new TypeError( error.message );
}

if ( 'className' in data ) {
forField += ` for ${data.className}${data.fieldName ? ( '::' + data.fieldName ) : '.constructor'}`; // eslint-disable-line prefer-template
}

super( `The following values are invalid${forField}: ${data.badValues.join( ', ' )}` );

this.data = data;
}
}

class HVMLRangeError extends HVMLTypeError {
/* className, field, expected, lowerBound, upperBound, exclusivity = 'inclusive' */
constructor( data ) {
Expand Down Expand Up @@ -208,7 +208,7 @@ class HVMLNotIntegerError extends HVMLTypeError {
}
}

module.exports = {
export default {
"DomainError": HVMLDomainError,
"EnumError": HVMLEnumError,
"TypeError": HVMLTypeError,
Expand Down
2 changes: 1 addition & 1 deletion util/validation.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Validation = require( './validation' );
const Validation = require( './validation.cjs' ).default;

describe( 'Validation', () => {
describe( 'HVMLDomainError', () => {
Expand Down
3 changes: 3 additions & 0 deletions video.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Set options as a parameter, environment variable, or rc file.
require = require( 'esm' )( module ); // eslint-disable-line no-global-assign
module.exports = require( './video.js' );
16 changes: 8 additions & 8 deletions video.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const isPlainObject = require( 'lodash/isPlainObject' );
const isNumber = require( 'lodash/isNumber' );
const isString = require( 'lodash/isString' );
const isUndefined = require( 'lodash/isUndefined' );
import isPlainObject from 'lodash/isPlainObject';
import isNumber from 'lodash/isNumber';
import isString from 'lodash/isString';
import isUndefined from 'lodash/isUndefined';

const Time = require( './util/time' );
const Validation = require( './util/validation' );
const Transform = require( './util/transform' );
import Time from './util/time';
import Validation from './util/validation';
import Transform from './util/transform';

class Video {
static isValidType( type ) {
Expand Down Expand Up @@ -400,4 +400,4 @@ class Video {
}
}

module.exports = Video;
export default Video;
4 changes: 2 additions & 2 deletions video.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Video = require( './video' );
const Validation = require( './util/validation' );
const Video = require( './video.js' );
const Validation = require( './util/validation.js' );

describe( 'Video', () => {
/* --- Instantiation --- */
Expand Down
Loading

0 comments on commit fd0d55f

Please sign in to comment.