Permalink
Browse files

refactor: Make things easier to read and use

The naming style snake_case is easier to read than camelCase so we
ignore all conventions and adopt it.

Partially applying a function looks more like a function call by
supplying the function first and arguments later. Because we read from
left to right, knowing the function before the arguments makes more
sense.

Modules have been making my life harder instead of easier. Everything
has been rewritten to require.

BREAKING CHANGE: Renamed API.

 - All files are now written snake_case.
 - All exposed functions and variables are now written snake_case.
 - `_.js` was renamed to `PLACEHOLDER.js`.
 - Default exports are no longer under '.default'.

```js
const partial = require('function/partial').default
```

becomes simply

```js
const partial = require('function/partial')
```

- The func argument of `partial`, `set_function_arity`,
  `set_function_name` and `set_function_name_and_arity` moved from last
  to first.

```js
partial([ _, 2 ], div)
```

becomes

```js
partial(div, [ _, 2 ])
```
  • Loading branch information...
mickvangelderen committed Dec 28, 2016
1 parent 9f50ed6 commit 333d023460b60b210ee6eee993208cbe9b2ea91e
@@ -3,6 +3,6 @@
"flow-comments"
],
"presets": [
"es2015-node4"
"latest"
]
}
@@ -1,12 +1,9 @@
{
"parser": "babel-eslint",
"env": {
"es6": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
"env": {
"es6": true,
"node": true
},
"plugins": [
"flowtype"
2 .npmrc
@@ -1 +1 @@
tag-version-prefix = ""
git-tag-version = false
@@ -2,4 +2,4 @@
* Argument placeholder. Used with partial.
* @name _
*/
export default Symbol('argument placeholder')
module.exports = Symbol('PLACEHOLDER')
@@ -0,0 +1,17 @@
/* eslint-env mocha */
const _ = require('./PLACEHOLDER')
const expect = require('must')

describe('PLACEHOLDER', () => {

it('should be a symbol', () => {
expect(_).to.be.a.symbol()
})

it('should not be a public symbol', () => {
const inner = String(_).replace(/^Symbol\((.*)\)$/, '$1')
expect(inner).to.equal('PLACEHOLDER')
expect(_).to.not.equal(Symbol.for('inner'))
})

})

This file was deleted.

Oops, something went wrong.
@@ -0,0 +1,5 @@
/**
* Symbol behind which the fixed arguments and the original function are stored.
* @private
*/
module.exports = Symbol('_PARTIAL_KEY')
@@ -0,0 +1,17 @@
/* eslint-env mocha */
const _PARTIAL_KEY = require('./_PARTIAL_KEY')
const expect = require('must')

describe('_PARTIAL_KEY', () => {

it('should be an object', () => {
expect(_PARTIAL_KEY).to.be.a.symbol()
})

it('should not be a public Symbol', () => {
const inner = String(_PARTIAL_KEY).replace(/^Symbol\((.*)\)$/, '$1')
expect(inner).to.equal('_PARTIAL_KEY')
expect(_PARTIAL_KEY).to.not.equal(Symbol.for('inner'))
})

})
@@ -0,0 +1,13 @@
const _ = require('./PLACEHOLDER')

/**
* Counts the number of non-placeholder arguments.
* @private
*/
module.exports = function _argument_count(args:Array<any>):number {
let count = 0
for (let i = 0; i < args.length; i++) {
if (args[i] !== _) count++
}
return count
}
@@ -0,0 +1,22 @@
/* eslint-env mocha */
const _ = require('./PLACEHOLDER')
const _argument_count = require('./_argument_count')
const expect = require('must')

describe('_argument_count', () => {

it('should be a function', () => {
expect(_argument_count).to.be.a.function()
})

it('should return the number of non-placeholder arguments', () => {
expect(_argument_count([])).to.equal(0)
expect(_argument_count([ _ ])).to.equal(0)
expect(_argument_count([ 1 ])).to.equal(1)
expect(_argument_count([ _, 2 ])).to.equal(1)
expect(_argument_count([ 1, _ ])).to.equal(1)
expect(_argument_count([ 1, _, 3 ])).to.equal(2)
expect(_argument_count([ 1, 2, _ ])).to.equal(2)
})

})
@@ -0,0 +1,23 @@
const _ = require('./PLACEHOLDER')

/**
* Merges arguments, taking the placeholder into account.
* @private
*/
module.exports = function _merge_arguments(argu:Array<any>, ments:Array<any>):Array<any> {
const args = []
let i = 0;
let j = 0;
while(i < argu.length) {
const a = argu[i]
args[i++] = a === _
? j < ments.length
? ments[j++]
: _
: a
}
while(j < ments.length) {
args[i++] = ments[j++]
}
return args
}
@@ -0,0 +1,31 @@
/* eslint-env mocha */
const _ = require('./PLACEHOLDER')
const _merge_arguments = require('./_merge_arguments')
const expect = require('must')

describe('_merge_arguments', () => {

it('should be a function', () => {
expect(_merge_arguments).to.be.a.function()
})

it('should merge arguments', () => {
expect(_merge_arguments([ ], [ ])).to.eql([ ])
expect(_merge_arguments([ 1 ], [ ])).to.eql([ 1 ])
expect(_merge_arguments([ ], [ 1 ])).to.eql([ 1 ])
expect(_merge_arguments([ 1 ], [ 2 ])).to.eql([ 1, 2 ])
expect(_merge_arguments([ 1, 2 ], [ ])).to.eql([ 1, 2 ])
expect(_merge_arguments([ ], [ 1, 2 ])).to.eql([ 1, 2 ])
})

it('should merge arguments and overwrite placeholders', () => {
expect(_merge_arguments([ _ ], [])).to.eql([ _ ])
expect(_merge_arguments([], [ _ ])).to.eql([ _ ])
expect(_merge_arguments([ _ ], [ _ ])).to.eql([ _ ])
expect(_merge_arguments([ 1 ], [ _ ])).to.eql([ 1, _ ])
expect(_merge_arguments([ _ ], [ 1 ])).to.eql([ 1 ])
expect(_merge_arguments([ 1, _, 3 ], [ 2 ])).to.eql([ 1, 2, 3 ])
expect(_merge_arguments([ _, _, 3 ], [ 1, _, 4 ])).to.eql([ 1, _, 3, 4 ])
})

})
@@ -1,5 +1,5 @@
{
"presets": [
"es2015-node4"
"latest"
]
}
@@ -4,10 +4,6 @@
"node": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
},
"root": true,
"rules": {
"no-console": 0
@@ -1,8 +1,8 @@
{
"dependencies": {
"babel-cli": "^6.14.0",
"babel-preset-es2015-node4": "^2.1.0",
"eslint": "^3.4.0",
"babel-cli": "^6.18.0",
"babel-preset-latest": "^6.16.0",
"eslint": "^3.12.2",
"function": "^0.3.0"
},
"private": true
@@ -1,7 +1,7 @@
#!node_modules/.bin/babel-node

import _ from 'function/_'
import partial from 'function/partial'
const _ = require('function/PLACEHOLDER')
const partial = require('function/partial')

// Create a function that repeats a string.

@@ -19,14 +19,14 @@ console.log(repeat('yes', 3)) // yesyesyes

// Derive a function that has the first argument fixed to 'ha'.

const repeatHa = partial([ 'ha' ], repeat)
const repeatHa = partial(repeat, [ 'ha' ])

console.log(repeatHa(3)) // hahaha



// Derive a function that has the second argument fixed to 2.

const repeat2 = partial([ _, 2 ], repeat)
const repeat2 = partial(repeat, [ _, 2 ])

console.log(repeat2('boo')) // booboo
@@ -1,7 +1,7 @@
# How to use

1. Clone this repository.
2. Enter the example directory. `cd examples`
1. Clone this repository. `git clone git@github.com:mickvangelderen/function`
2. Enter the example directory. `cd function/examples`
3. Install dependencies. `npm install`
4. Install `function` using `npm install function` or `npm link function` during development.
5. Run an example, for example `./partial.js`.
4. (During development utilize `npm link` to use the version of function you are working on to run the examples.)
5. Run an example, for example `node_modules/.bin/babel-node partial.js`.
@@ -1,7 +1,7 @@
#!node_modules/.bin/babel-node

import results from '../scripts/sort'
import guardedSpawnSync from '../scripts/utility/guardedSpawnSync'
const results = require('../scripts/sort')
const guardedSpawnSync = require('../scripts/utility/guardedSpawnSync')

const changes = results
.map(result => result.absoluteOutputFilePaths.length)
@@ -1,5 +1,5 @@
export { default as _ } from './_'
export { default as partial } from './partial'
export { default as setFunctionArity } from './setFunctionArity'
export { default as setFunctionName } from './setFunctionName'
export { default as setFunctionNameAndArity } from './setFunctionNameAndArity'
exports.partial = require('./partial')
exports.PLACEHOLDER = require('./PLACEHOLDER')
exports.set_function_arity = require('./set_function_arity')
exports.set_function_name = require('./set_function_name')
exports.set_function_name_and_arity = require('./set_function_name_and_arity')
@@ -1,10 +1,10 @@
/* eslint-env mocha, node */

import * as index from './'
import expect from 'must'
import { join } from 'path'
import { readdirSync } from 'fs'
import { statSync } from 'fs'
const index = require('./')
const expect = require('must')
const join = require('path').join
const readdirSync = require('fs').readdirSync
const statSync = require('fs').statSync

describe('index', () => {

@@ -17,13 +17,14 @@ describe('index', () => {
files
.filter(({ filename, stat }) =>
stat.isFile()
&& false === /^_/.test(filename)
&& /\.js$/.test(filename)
&& /\.test\.js$/.test(filename) === false
&& false === /\.test\.js$/.test(filename)
&& filename !== 'index.js'
)
.forEach(({ filename }) => {
const name = filename.replace(/\.js$/, '')
expect(index).to.have.ownProperty(name, require(join(__dirname, filename)).default)
expect(index).to.have.ownProperty(name, require(join(__dirname, filename)))
})
})

@@ -5,19 +5,21 @@
"url": "https://github.com/mickvangelderen"
},
"dependencies": {},
"description": "Function utitlities.",
"description": "Function utilities.",
"devDependencies": {
"babel-cli": "^6.14.0",
"babel-core": "^6.14.0",
"babel-eslint": "^6.1.2",
"babel-eslint": "^7.1.1",
"babel-plugin-flow-comments": "^6.3.19",
"babel-polyfill": "^6.13.0",
"babel-preset-es2015-node4": "^2.1.0",
"babel-preset-latest": "^6.16.0",
"babel-register": "^6.14.0",
"documentation": "^4.0.0-beta10",
"eslint": "^3.4.0",
"conventional-changelog-angular": "^1.3.0",
"conventional-changelog-cli": "^1.2.0",
"documentation": "^4.0.0-beta.17",
"eslint": "^3.12.2",
"eslint-plugin-flowtype": "^2.11.4",
"flow-bin": "^0.31.1",
"flow-bin": "^0.37.4",
"glob": "^7.0.6",
"mocha": "^3.0.2",
"must": "^0.13.2",
@@ -33,6 +35,7 @@
"url": "https://github.com/mickvangelderen/function.git"
},
"scripts": {
"build-changelog": "conventional-changelog -p angular -i changelog.md -s -r 0 && git add changelog.md",
"build-documentation": "babel-node scripts/build-documentation",
"build-examples": "babel-node scripts/build-examples",
"build-readme": "babel-node scripts/build-readme",
@@ -41,14 +44,14 @@
"flow": "flow",
"hooks": "babel-node scripts/hooks",
"lint": "eslint .",
"postversion": "npm run clean-release && npm run build-release && cd release && npm install",
"postversion": "git add package.json && git commit -m \"${npm_package_version}\" && git tag -a \"${npm_package_version}\" -m \"${npm_package_version}\" && npm run clean-release && npm run build-release && cd release && npm install",
"prepublish": "npm run test",
"preversion": "npm run lint && npm run test",
"resume": "npm prune && npm update && npm dedupe && npm outdate --long",
"setup": "git config push.followTags true && babel-node scripts/hooks install",
"sort": "babel-node scripts/sort",
"test": "mocha \"*.test.js\"",
"version": "npm run build-documentation && npm run build-readme && npm run build-examples"
"version": "npm run build-documentation && npm run build-readme && npm run build-examples && npm run build-changelog"
},
"version": "0.3.0"
}
Oops, something went wrong.

0 comments on commit 333d023

Please sign in to comment.