Skip to content

Commit

Permalink
Publish
Browse files Browse the repository at this point in the history
  • Loading branch information
ekoeryanto committed May 11, 2018
0 parents commit 4469d97
Show file tree
Hide file tree
Showing 11 changed files with 4,492 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"presets": [
["env", {
"targets": {
"node": "4.0.0"
}
}]
],
"plugins": [
"add-module-exports"
]
}
23 changes: 23 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# http://editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

# The JSON files contain newlines inconsistently
[*.json]
insert_final_newline = ignore

# Minified JavaScript files shouldn't be changed
[**.min.js]
indent_style = ignore
insert_final_newline = ignore

[*.md]
trim_trailing_whitespace = false

6 changes: 6 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "standard",
"env": {
"jest": true
}
}
99 changes: 99 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# File created using '.gitignore Generator' for Visual Studio Code: https://bit.ly/vscode-gig

# Created by https://www.gitignore.io/api/node,visualstudiocode,windows

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
.history

### Windows ###
# Windows thumbnail cache files
Thumbs.db
ehthumbs.db
ehthumbs_vista.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows shortcuts
*.lnk


# End of https://www.gitignore.io/api/node,visualstudiocode,windows

# Custom rules (everything added below won't be overriden by 'Generate .gitignore File' if you use 'Update' option)

/index.js
31 changes: 31 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "module-igniter",
"version": "0.1.0",
"description": "Node module loader and executor.",
"main": "index.js",
"repository": "https://github.com/ekoeryanto/module-igniter.git",
"author": "Eko Eryanto <ekoeryanto@gmail.com>",
"license": "MIT",
"scripts": {
"prebuild": "yarn test",
"build": "babel src -d .",
"test": "jest",
"prepublish": "yarn build"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-preset-env": "^1.7.0",
"eslint": "^4.19.1",
"eslint-config-standard": "^11.0.0",
"eslint-plugin-import": "^2.11.0",
"eslint-plugin-node": "^6.0.1",
"eslint-plugin-promise": "^3.7.0",
"eslint-plugin-standard": "^3.1.0",
"jest": "^22.4.3"
},
"eslintIgnore": [
"/index.js"
]
}
45 changes: 45 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
export default options => {
const prefix = options.prefix || ''

return (...plugs) => {
if (plugs.length < 1) throw new Error('No module to load')

const last = plugs[plugs.length - 1]

// test will always true when param is array/object or non empty string
let test = Boolean(last)

// if test is true, check for boolean string
if (test && /false|true|\d/.test(last)) {
test = Boolean(JSON.parse(last))
plugs.pop()
}

if (!test) return []

return (
plugs
// remove dupe, empty and boolean(test) value
.filter((v, i, a) => a.indexOf(v) === i && typeof v !== 'boolean' && v)
.map(p => {
if (typeof p === 'string') {
return require(prefix + p)()
} else if (Array.isArray(p)) {
return p.map(name => require(prefix + name)())
}
return Object.keys(p)
.filter(x => p[x])
.map(
z =>
p[z] === true
? require(prefix + z)()
: Array.isArray(p[z])
? require(prefix + z)(...p[z])
: require(prefix + z)(p[z])
)
})
// flatten array
.reduce((a, c) => a.concat(c), [])
)
}
}
112 changes: 112 additions & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
const igniter = require('../src')

const plug = igniter({ prefix: '../tests/mocks/' })

it('should throw if no param passed', () => {
expect(plug).toThrowError('No module to load')
})

describe('without argument', () => {
const expected = [
{
loaded: true,
option: [],
name: expect.stringMatching(/foo|bar|noop/)
}
]

test('ignite by string params', () => {
expect(plug('noop', 'foo')).toEqual(expect.arrayContaining(expected))
})
test('ignited module count', () => {
expect(plug('noop', 'foo')).toHaveLength(2)
})
test('ignite by array params', () => {
expect(plug(['noop', 'foo'])).toEqual(expect.arrayContaining(expected))
})
test('ignited module count', () => {
expect(plug(['noop', 'foo'])).toHaveLength(2)
})
test('ignite by boolean object params', () => {
expect(plug({ noop: true, foo: true })).toEqual(
expect.arrayContaining(expected)
)
})
test('ignited module count', () => {
expect(plug({ noop: true, foo: true })).toHaveLength(2)
})
test('ignited by boolean false object ommited', () => {
expect(plug({ noop: true, foo: false })).toHaveLength(1)
})
test('duplicate module ommited', () => {
expect(plug('noop', 'foo', 'noop')).toHaveLength(2)
})

describe('test (last) param', () => {
describe('falsy have no module', () => {
test('0', () => {
expect(plug('foo', 'noop', 0)).toHaveLength(0)
})
test('`0`', () => {
expect(plug('foo', 'noop', `0`)).toHaveLength(0)
})
test('false', () => {
expect(plug('foo', 'noop', false)).toHaveLength(0)
})
test('`false`', () => {
expect(plug('foo', 'noop', `false`)).toHaveLength(0)
})
test('undefined', () => {
expect(plug('foo', 'noop', undefined)).toHaveLength(0)
})
test('null', () => {
expect(plug('foo', 'noop', null)).toHaveLength(0)
})
test('``', () => {
expect(plug('foo', 'noop', '')).toHaveLength(0)
})
test('NaN', () => {
expect(plug('foo', 'noop', NaN)).toHaveLength(0)
})
})

describe('truthy have the modules', () => {
test('1', () => {
expect(plug('foo', 'noop', 1)).toHaveLength(2)
})
test('`1`', () => {
expect(plug('foo', 'noop', `1`)).toHaveLength(2)
})
test('true', () => {
expect(plug('foo', 'noop', true)).toHaveLength(2)
})
test('`true`', () => {
expect(plug('foo', 'noop', `true`)).toHaveLength(2)
})
})
})
})

describe('with argument', () => {
const expected = option =>
expect.arrayContaining([
expect.objectContaining({
name: expect.stringMatching(/foo|bar|noop/),
loaded: true,
option: expect.arrayContaining(option)
})
])
it('should have single option object in module', () => {
expect(plug({ foo: { baz: 1 } })).toEqual(
expected([expect.objectContaining({ baz: 1 })])
)
})
it('should have multiple option object in module', () => {
expect(plug({ foo: [{ baz: 1 }, { bax: 1 }] })).toEqual(
expected([
expect.objectContaining({ baz: 1 }),
expect.objectContaining({ bax: 1 })
])
)
})
})
7 changes: 7 additions & 0 deletions tests/mocks/bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = (...option) => {
return {
name: 'bar',
loaded: true,
option
}
}
7 changes: 7 additions & 0 deletions tests/mocks/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = (...option) => {
return {
name: 'foo',
loaded: true,
option
}
}
7 changes: 7 additions & 0 deletions tests/mocks/noop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = (...option) => {
return {
name: 'noop',
loaded: true,
option
}
}
Loading

0 comments on commit 4469d97

Please sign in to comment.