Skip to content

Commit

Permalink
feat: Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Duarte committed Jun 5, 2018
0 parents commit 301a8b9
Show file tree
Hide file tree
Showing 11 changed files with 5,007 additions and 0 deletions.
69 changes: 69 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# 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 (https://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

# next.js build output
.next

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless

/dist
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package.json
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"semi": false,
"trailingComma": "all"
}
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: node_js
node_js:
- node
script:
- yarn ci
after_success:
- yarn release
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# pipe-now

A tiny lib (less than 500 bytes) that simulates the behavior of the [pipeline-operator proposal](https://github.com/tc39/proposal-pipeline-operator).

# How to use

Pretty much the same way you would use the pipeline operator, but with a function. The first value will be the initial value, and the rest of the arguments are the functions to be piped

Check out the example:

```js
import pipe from "pipe-now"

const uppercase = val => val.toUpperCase()
const topifySpaces = val => val.replace(/ /g, " 👌 ")

pipe(
256,
Math.sqrt,
Math.sqrt,
) // result: 4

pipe(
256,
Math.sqrt,
val => val + 2,
) // result: 18

pipe(
"glory to the pipes",
topifySpaces,
uppercase,
) // result: 'GLORY 👌 TO 👌 THE 👌 PIPES'
```

# FAQ

## Why not just use [`_.flow`](lodash.com/docs/#flow)

`_.flow` returns a function for you to pass the initial value, which is different from the pipeline operator proposal that only returns the final value, and you can pass the first argument as the initial value.

You could use flow with some quirks (`_.flow(() => "initial value", val => val.toUpperCase())()`), but in my opinion, this is quite ugly, so I decided to just make this lib.
73 changes: 73 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"name": "pipe-now",
"version": "0.0.0-development",
"description": "A pipe function that acts like the pipeline operator proposal.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"author": "Gabriel Duarte <[email protected]> (https://www.gabrielduarte.tech/)",
"license": "MIT",
"homepage": "https://github.com/GabrielDuarteM/pipe-now",
"keywords": [
"pipe",
"now",
"pipeline",
"operator",
"tool",
"tools",
"util",
"utils",
"helper",
"helpers"
],
"files": [
"dist"
],
"repository": {
"type": "git",
"url": "https://github.com/GabrielDuarteM/pipe-now.git"
},
"bugs": {
"url": "https://github.com/GabrielDuarteM/pipe-now/issues"
},
"scripts": {
"clean": "rimraf dist",
"start": "yarn build -w",
"build": "yarn clean && tsc",
"test": "jest",
"lint": "tslint --project tsconfig.json",
"ci": "yarn lint && yarn test && yarn build",
"release": "semantic-release"
},
"jest": {
"moduleFileExtensions": [
"ts",
"tsx",
"js"
],
"transform": {
"^.+\\.(ts|tsx)$": "ts-jest"
},
"globals": {
"ts-jest": {
"tsConfigFile": "tsconfig.json"
}
},
"testMatch": [
"**/*.test.+(ts|tsx|js)"
]
},
"devDependencies": {
"@types/jest": "^23.0.0",
"@types/node": "^10.3.1",
"jest": "^23.1.0",
"prettier": "^1.13.4",
"rimraf": "^2.6.2",
"semantic-release": "^15.5.0",
"ts-jest": "^22.4.6",
"tslint": "^5.10.0",
"tslint-config-prettier": "^1.13.0",
"tslint-consistent-codestyle": "^1.13.1",
"tslint-plugin-prettier": "^1.3.0",
"typescript": "^2.9.1"
}
}
58 changes: 58 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* tslint:disable no-magic-numbers */

import pipe from "./"

describe("pipe", () => {
// describe("deve passar o primeiro argumento passado pro pipe para as funções seguintes e retornar o resultado da ultima função", () => {
it("should return 16 when the following parameters are passed: (256, Math.sqrt)", () => {
const initialValue = 256
const expected = 16
const actual = pipe(
initialValue,
Math.sqrt,
)

expect(actual).toBe(expected)
})

it("should return 4 when the following parameters are passed: (256, Math.sqrt, Math.sqrt)", () => {
const initialValue = 256
const expected = 4
const actual = pipe(
initialValue,
Math.sqrt,
Math.sqrt,
)

expect(actual).toBe(expected)
})

it("should return 10 when the following parameters are passed: (256, Math.sqrt, Math.sqrt, val => val + 6)", () => {
const initialValue = 256
const expected = 10
const actual = pipe(
initialValue,
Math.sqrt,
Math.sqrt,
value => value + 6,
)

expect(actual).toBe(expected)
})

it('should return "GLORY 👌 TO 👌 THE 👌 PIPES" when the following parameters are passed: ("glory to the pipes", topifySpaces, uppercase) ', () => {
const initialValue = "glory to the pipes"
const expected = "GLORY 👌 TO 👌 THE 👌 PIPES"

const uppercase = val => val.toUpperCase()
const topifySpaces = val => val.replace(/ /g, " 👌 ")

const actual = pipe(
initialValue,
topifySpaces,
uppercase,
)

expect(actual).toBe(expected)
})
})
11 changes: 11 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const pipe = <T>(initialValue: T, ...args: any[]) => {
const finalValue = args.reduce((value, func) => {
const nextValue = func(value)

return nextValue
}, initialValue)

return finalValue
}

export default pipe
18 changes: 18 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"declaration": true,
"sourceMap": true,
"outDir": "./dist",
"strict": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"baseUrl": ".",
"paths": {
"*": ["node_modules/*", "src/types/*"]
}
},
"include": ["src/**/*"],
"exclude": ["src/**/*.test.*"]
}
68 changes: 68 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"extends": ["tslint:recommended", "tslint-config-prettier"],
"rulesDirectory": ["tslint-plugin-prettier", "tslint-consistent-codestyle"],
"rules": {
"no-magic-numbers": true,
"only-arrow-functions": true,
"await-promise": [true, "Thenable"],
"ban-comma-operator": true,
"no-duplicate-switch-case": true,
"no-dynamic-delete": true,
"no-floating-promises": true,
"no-implicit-dependencies": true,
"no-invalid-template-strings": true,
"no-invalid-this": true,
"no-object-literal-type-assertion": true,
"no-return-await": true,
"no-sparse-arrays": true,
"no-switch-case-fall-through": true,
"no-this-assignment": true,
"no-unnecessary-class": true,
"no-unused-variable": true,
"no-void-expression": [true, "ignore-arrow-function-shorthand"],
"prefer-conditional-expression": true,
"prefer-object-spread": true,
"restrict-plus-operands": true,
"strict-type-predicates": true,
"switch-default": true,
"use-default-type-parameter": true,
"cyclomatic-complexity": true,
"deprecation": true,
"indent": [true, "spaces", 2],
"no-duplicate-imports": true,
"no-require-imports": true,
"prefer-readonly": true,
"binary-expression-operand-order": true,
"match-default-export-name": true,
"newline-before-return": true,
"no-boolean-literal-compare": true,
"no-irregular-whitespace": true,
"no-unnecessary-qualifier": true,
"number-literal-format": true,
"prefer-switch": true,
"prefer-template": true,
"return-undefined": true,
"variable-name": [true, "ban-keywords", "check-format"],
"no-var-keyword": true,
"no-parameter-reassignment": true,
"typedef": [true, "call-signature"],

/**
* Enables tslint-plugin-prettier
*/
"prettier": true,

/**
* tslint-consistent-codestyle rules
* https://github.com/ajafff/tslint-consistent-codestyle#rules
*/

"early-exit": true,
"no-accessor-recursion": true,
"no-collapsible-if": true,
"no-unnecessary-else": true,
"object-shorthand-properties-first": true,
"prefer-const-enum": true,
"prefer-while": true
}
}
Loading

0 comments on commit 301a8b9

Please sign in to comment.