Code files

This commit is contained in:
Akhil
2017-07-31 11:33:31 +05:30
parent 073a63ebdd
commit b9ae409ec0
801 changed files with 17535 additions and 0 deletions
@@ -0,0 +1,3 @@
module.exports = (age, gap) => {
return `In ${gap} years you will be ${Number(age) + gap}<br>`
}
@@ -0,0 +1,10 @@
const express = require('express')
const app = express()
const past = require('./past')
const future = require('./future')
app.get('/:age', (req, res) => {
res.send(past(req.params.age, 10) + future(req.params.future, 10))
})
app.listen(3000)
@@ -0,0 +1,15 @@
{
"name": "app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.15.2"
}
}
@@ -0,0 +1,3 @@
module.exports = (age, gap) => {
return `${gap} years ago you were ${Number(age) - gap}<br>`
}
@@ -0,0 +1,11 @@
const express = require('express')
const app = express()
app.get('/', (req, res) => res.send('hey'))
setTimeout(function myTimeout() {
console.log('I waited for you.')
}, 100)
app.listen(3000)
@@ -0,0 +1,15 @@
{
"name": "app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.15.0"
}
}
@@ -0,0 +1,16 @@
const util = require('util')
const express = require('express')
const debug = util.debuglog('my-app')
const app = express()
app.get('/', (req, res) => {
debug('incoming request on /', req.route)
res.send('hey')
})
setTimeout(function myTimeout() {
debug('timeout complete')
console.log('I waited for you.')
}, 100)
app.listen(3000)
@@ -0,0 +1,15 @@
{
"name": "app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.15.0"
}
}
@@ -0,0 +1,13 @@
const express = require('express')
const app = express()
const stylus = require('stylus')
app.get('/some.css', (req, res) => {
const css = stylus(`
body
color:black
`).render()
res.send(css)
})
app.listen(3000)
@@ -0,0 +1,16 @@
{
"name": "app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.15.0",
"stylus": "^0.54.5"
}
}
@@ -0,0 +1,15 @@
const express = require('express')
const app = express()
const stylus = require('stylus')
const debug = require('debug')('my-app')
app.get('/some.css', (req, res) => {
debug('css requested')
const css = stylus(`
body
color:black
`).render()
res.send(css)
})
app.listen(3000)
@@ -0,0 +1,17 @@
{
"name": "app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"debug": "^2.6.2",
"express": "^4.15.0",
"stylus": "^0.54.5"
}
}
@@ -0,0 +1,13 @@
const express = require('express')
const app = express()
const stylus = require('stylus')
app.get('/some.css', (req, res) => {
const css = stylus(`
body
color:black
`).render()
res.send(css)
})
app.listen(3000)
@@ -0,0 +1,19 @@
{
"name": "app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "node index.js",
"prod": "node -r pino-debug index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.15.0",
"pino-debug": "^1.0.3",
"stylus": "^0.54.5"
}
}
@@ -0,0 +1,18 @@
{
"name": "app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.15.0",
"pino-debug": "^1.0.2",
"stylus": "^0.54.5"
}
}
@@ -0,0 +1,5 @@
function content (opts, c = 20) {
return --c ? content(opts, c) : opts.ohoh
}
module.exports = content
@@ -0,0 +1,7 @@
const express = require('express')
const routes = require('./routes')
const app = express()
app.use(routes)
app.listen(3000)
@@ -0,0 +1,15 @@
{
"name": "app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.15.2"
}
}
@@ -0,0 +1,9 @@
const content = require('./content')
const {Router} = require('express')
const router = new Router()
router.get('/', (req, res) => {
res.send(content())
})
module.exports = router
@@ -0,0 +1,9 @@
function content (opts, c = 20) {
function produce (cb) {
if (--c) setTimeout(produce, 10, cb)
cb(null, opts.ohoh)
}
return produce
}
module.exports = content
@@ -0,0 +1,10 @@
if (process.env.NODE_ENV !== 'production') {
require('longjohn')
}
const express = require('express')
const routes = require('./routes')
const app = express()
app.use(routes)
app.listen(3000)
@@ -0,0 +1,18 @@
{
"name": "app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.15.2"
},
"devDependencies": {
"longjohn": "^0.2.12"
}
}
@@ -0,0 +1,15 @@
const content = require('./content')
const {Router} = require('express')
const router = new Router()
router.get('/', (req, res) => {
content()((err, html) => {
if (err) {
res.send(500)
return
}
res.send(html)
})
})
module.exports = router
@@ -0,0 +1,5 @@
function content (opts, c = 20) {
return --c ? content(opts, c) : opts.ohoh
}
module.exports = content
@@ -0,0 +1,10 @@
if (process.env.NODE_ENV !== 'production') {
Error.stackTraceLimit = Infinity
}
const express = require('express')
const routes = require('./routes')
const app = express()
app.use(routes)
app.listen(3000)
@@ -0,0 +1,15 @@
{
"name": "app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.15.2"
}
}
@@ -0,0 +1,9 @@
const content = require('./content')
const {Router} = require('express')
const router = new Router()
router.get('/', (req, res) => {
res.send(content())
})
module.exports = router
@@ -0,0 +1,5 @@
function content (opts, c = 20) {
return --c ? content(opts, c) : opts.ohoh
}
module.exports = content
@@ -0,0 +1,8 @@
require('cute-stack')()
const express = require('express')
const routes = require('./routes')
const app = express()
app.use(routes)
app.listen(3000)
@@ -0,0 +1,17 @@
{
"name": "app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"cute-stack": "^1.4.3",
"express": "^4.15.2"
},
"devDependencies": {}
}
@@ -0,0 +1,9 @@
const content = require('./content')
const {Router} = require('express')
const router = new Router()
router.get('/', (req, res) => {
res.send(content())
})
module.exports = router
@@ -0,0 +1,22 @@
{
"name": "hsl-to-hex",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "David Mark Clements",
"license": "ISC",
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/davidmarkclements/hsl-to-hex.git"
},
"bugs": {
"url": "https://github.com/davidmarkclements/hsl-to-hex/issues"
},
"homepage": "https://github.com/davidmarkclements/hsl-to-hex#readme",
"description": "",
"dependencies": {
"hsl-to-rgb-for-reals": "^1.1.0"
}
}
@@ -0,0 +1,25 @@
{
"name": "hsl-to-hex",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "David Mark Clements",
"license": "ISC",
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/davidmarkclements/hsl-to-hex.git"
},
"bugs": {
"url": "https://github.com/davidmarkclements/hsl-to-hex/issues"
},
"homepage": "https://github.com/davidmarkclements/hsl-to-hex#readme",
"description": "",
"dependencies": {
"hsl-to-rgb-for-reals": "^1.1.0"
},
"devDependencies": {
"standard": "^6.0.8"
}
}
@@ -0,0 +1,26 @@
{
"name": "hsl-to-hex",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "npm run lint",
"lint": "standard"
},
"author": "David Mark Clements",
"license": "ISC",
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/davidmarkclements/hsl-to-hex.git"
},
"bugs": {
"url": "https://github.com/davidmarkclements/hsl-to-hex/issues"
},
"homepage": "https://github.com/davidmarkclements/hsl-to-hex#readme",
"description": "",
"dependencies": {
"hsl-to-rgb-for-reals": "^1.1.0"
},
"devDependencies": {
"standard": "^6.0.8"
}
}
@@ -0,0 +1,2 @@
node_modules
*.log
@@ -0,0 +1,6 @@
var hsl = require('./')
var hue = 133
var saturation = 40
var luminosity = 60
var hex = hsl(hue, saturation, luminosity)
console.log(hex) // #70c282
@@ -0,0 +1,66 @@
// In our case, there's only one dependency
var toRgb = require('hsl-to-rgb-for-reals')
var debug = require('debug')('hsl-to-hex')
// Typically all dependencies should be declared at the top of the file.
// Now let's define an API for our module, we're taking hue, saturation and luminosity values and outputting a CSS compatible hex string.
// Hue is in degrees, between 0 and 359. Since degrees a cyclical in nature, we'll support numbers greater than 359 or less than 0 by "spinning" them around until they fall within the 0 to 359 range.
// Saturation and luminosity are both percentages, we'll represent these percentages with whole numbers between 0 and 100. For these numbers we'll need to enforce a maximum and a minimum, anything below 0 will become 0, anything above 100 will become 100.
// Let's write some utility functions to handle this logic:
function max (val, n) {
debug('ensuring ' + val + ' is no more than ' + n)
return (val > n) ? n : val
}
function min (val, n) {
debug('ensuring ' + val + ' is no less than ' + n)
return (val < n) ? n : val
}
function cycle (val) {
debug('resolving ' + val + ' within the 0-359 range')
// for safety:
val = max(val, 1e7)
val = min(val, -1e7)
// cycle value:
while (val < 0) { val += 360 }
while (val > 359) { val -= 360 }
return val
}
// Now for the main piece, the `hsl` function:
function hsl (hue, saturation, luminosity) {
debug('calculating hex for hue: ' + hue + ' saturation: ' + saturation + ' luminosity: ' + luminosity)
// resolve degrees to 0 - 359 range
hue = cycle(hue)
// enforce constraints
saturation = min(max(saturation, 100), 0)
luminosity = min(max(luminosity, 100), 0)
// convert to 0 to 1 range used by hsl-to-rgb-for-reals
saturation /= 100
luminosity /= 100
// let hsl-to-rgb-for-reals do the hard work
var rgb = toRgb(hue, saturation, luminosity)
// convert each value in the returned RGB array
// to a 2 character hex value, join the array into
// a string, prefixed with a hash
return '#' + rgb
.map(function (n) {
return (256 + n).toString(16).substr(-2)
})
.join('')
}
// In order to make our code into a bona fide module we have to export it:
module.exports = hsl
@@ -0,0 +1,33 @@
{
"name": "@davidmarkclements/hsl-to-hex",
"version": "1.0.1",
"main": "index.js",
"scripts": {
"check": "npm ls && npm test",
"test": "npm run lint && tap --cov test",
"lint": "standard",
"prepublish": "npm run check && ./publish-dep.sh"
},
"author": "David Mark Clements",
"license": "ISC",
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/davidmarkclements/hsl-to-hex.git"
},
"bugs": {
"url": "https://github.com/davidmarkclements/hsl-to-hex/issues"
},
"homepage": "https://github.com/davidmarkclements/hsl-to-hex#readme",
"description": "Convert HSL colors to RGB colors in hex format.",
"dependencies": {
"debug": "^2.2.0",
"hsl-to-rgb-for-reals": "^1.1.0"
},
"devDependencies": {
"standard": "^6.0.8",
"tap": "^5.7.0"
},
"directories": {
"test": "test"
}
}
@@ -0,0 +1,50 @@
#! /bin/sh
# 400 - Missing hash and/or peer_id
# 403 - Not allowed
# 413 - Too big
# 200 - Ok!
if echo $npm_config_argv | grep -q "install"; then
echo "Running at install step, skipping"
else
if ipfs &>/dev/null; then
echo "## Publishing dependency"
mv node_modules .node_modules 2>/dev/null
HASH=$(ipfs add . -r | tail -n 1 | cut -d ' ' -f 2)
mv .node_modules node_modules 2>/dev/null
echo "Published as $HASH"
PEER=$(ipfs id --format '<id>')
if [ -f ~/.stay/nodes.json ]; then
cat ~/.stay/nodes.json | jq -rc '.[]' | while read host; do
address="$host/api/pin/add/$HASH/$PEER"
status=$(curl -X POST --silent $address)
case "$status" in
"400") echo "$host - Application Error: Missing the hash and/or peer_id"
;;
"403") echo "$host - You do not have access to pinning at this node"
;;
"413") echo "$host - The module was too big to pin!"
;;
"200") echo "$host - Pinned!"
;;
*) echo "Weird status code $status for $host"
;;
esac
done
else
echo "You don't have any saved nodes in ~/.stay/nodes.json, skip pinning"
fi
else
echo "## Could not publish dependency to IPFS, doing the good'ol 'fetch from npm registry' way"
echo "Either 'ipfs' doesn't exists in PATH or you haven't run 'ipfs daemon' before running the command"
exit 0
fi
fi
@@ -0,0 +1,31 @@
# hsl-to-hex
Convert HSL colors to RGB colors in hex format.
## Install
```sh
npm install --save @davidmarkclements/hsl-to-hex
```
## API
```
require('hsl-to-hex') => Function
hsl(hue, saturation, luminosity)` => String
```
## Example
```js
var hsl = require('hsl-to-hex')
var hue = 133
var saturation = 40
var luminosity = 60
var hex = hsl(hue, saturation, luminosity)
console.log(hex) // #70c282
```
## License
ISC
@@ -0,0 +1,74 @@
var hsl = require('../')
var test = require('tap').test
test('pure white', function (assert) {
var expected = '#ffffff'
var actual = hsl(0, 100, 100)
var it = 'max saturation and luminosity should return pure white'
assert.is(actual, expected, it)
assert.end()
})
test('medium gray', function (assert) {
var expected = '#808080'
var actual = hsl(0, 0, 50)
var it = '0% saturation, 50% luminosity should be medium gray'
assert.is(actual, expected, it)
assert.end()
})
test('hue - red', function (assert) {
var expected = '#ff0000'
var actual = hsl(0, 100, 50)
var it = '0deg should be red'
assert.is(actual, expected, it)
assert.end()
})
test('hue - blue', function (assert) {
var expected = '#0000ff'
var actual = hsl(240, 100, 50)
var it = '240deg should be blue'
assert.is(actual, expected, it)
assert.end()
})
test('hue - cyan', function (assert) {
var expected = '#00ffff'
var actual = hsl(180, 100, 50)
var it = '180deg should be cyan'
assert.is(actual, expected, it)
assert.end()
})
test('degree overflow', function (assert) {
var expected = hsl(1, 100, 50)
var actual = hsl(361, 100, 50)
var it = '361deg should be the same as 1deg'
assert.is(actual, expected, it)
assert.end()
})
test('degree underflow', function (assert) {
var expected = hsl(-1, 100, 50)
var actual = hsl(359, 100, 50)
var it = '-1deg should be the same as 359deg'
assert.is(actual, expected, it)
assert.end()
})
test('max constraint', function (assert) {
var expected = hsl(0, 101, 50)
var actual = hsl(0, 100, 50)
var it = '101% should be the same as 100%'
assert.is(actual, expected, it)
assert.end()
})
test('min constraint', function (assert) {
var expected = hsl(0, -1, 50)
var actual = hsl(0, 0, 50)
var it = '-1% should be the same as 0%'
assert.is(actual, expected, it)
assert.end()
})
@@ -0,0 +1,11 @@
{
"name": "stay-play",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "David Mark Clements",
"license": "MIT"
}
@@ -0,0 +1,2 @@
node_modules
*.log
@@ -0,0 +1,6 @@
var hsl = require('./')
var hue = 133
var saturation = 40
var luminosity = 60
var hex = hsl(hue, saturation, luminosity)
console.log(hex) // #70c282
@@ -0,0 +1,59 @@
// In our case, there's only one dependency
var toRgb = require('hsl-to-rgb-for-reals')
// Typically all dependencies should be declared at the top of the file.
// Now let's define an API for our module, we're taking hue, saturation and luminosity values and outputting a CSS compatible hex string.
// Hue is in degrees, between 0 and 359. Since degrees a cyclical in nature, we'll support numbers greater than 359 or less than 0 by "spinning" them around until they fall within the 0 to 359 range.
// Saturation and luminosity are both percentages, we'll represent these percentages with whole numbers between 0 and 100. For these numbers we'll need to enforce a maximum and a minimum, anything below 0 will become 0, anything above 100 will become 100.
// Let's write some utility functions to handle this logic:
function max (val, n) {
return (val > n) ? n : val
}
function min (val, n) {
return (val < n) ? n : val
}
function cycle (val) {
// for safety:
val = max(val, 1e7)
val = min(val, -1e7)
// cycle value:
while (val < 0) { val += 360 }
while (val > 359) { val -= 360 }
return val
}
// Now for the main piece, the `hsl` function:
function hsl (hue, saturation, luminosity) {
// resolve degrees to 0 - 359 range
hue = cycle(hue)
// enforce constraints
saturation = min(max(saturation, 100), 0)
luminosity = min(max(luminosity, 100), 0)
// convert to 0 to 1 range used by hsl-to-rgb-for-reals
saturation /= 100
luminosity /= 100
// let hsl-to-rgb-for-reals do the hard work
var rgb = toRgb(hue, saturation, luminosity)
// convert each value in the returned RGB array
// to a 2 character hex value, join the array into
// a string, prefixed with a hash
return '#' + rgb
.map(function (n) {
return (256 + n).toString(16).substr(-2)
})
.join('')
}
// In order to make our code into a bona fide module we have to export it:
module.exports = hsl
@@ -0,0 +1,30 @@
{
"name": "@davidmarkclements/hsl-to-hex",
"version": "1.0.1",
"main": "index.js",
"scripts": {
"test": "npm run lint && tap --cov test",
"lint": "standard"
},
"author": "David Mark Clements",
"license": "ISC",
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/davidmarkclements/hsl-to-hex.git"
},
"bugs": {
"url": "https://github.com/davidmarkclements/hsl-to-hex/issues"
},
"homepage": "https://github.com/davidmarkclements/hsl-to-hex#readme",
"description": "Convert HSL colors to RGB colors in hex format.",
"dependencies": {
"hsl-to-rgb-for-reals": "^1.1.0"
},
"devDependencies": {
"standard": "^6.0.8",
"tap": "^5.7.0"
},
"directories": {
"test": "test"
}
}
@@ -0,0 +1,32 @@
# hsl-to-hex
Convert HSL colors to RGB colors in hex format.
## Install
```sh
npm install --save @davidmarkclements/hsl-to-hex
```
## API
```
require('hsl-to-hex') => Function
hsl(hue, saturation, luminosity)` => String
```
## Example
```js
var hsl = require('hsl-to-hex')
var hue = 133
var saturation = 40
var luminosity = 60
var hex = hsl(hue, saturation, luminosity)
console.log(hex) // #70c282
```
## License
ISC
@@ -0,0 +1,74 @@
var hsl = require('../')
var test = require('tap').test
test('pure white', function (assert) {
var expected = '#ffffff'
var actual = hsl(0, 100, 100)
var it = 'max saturation and luminosity should return pure white'
assert.is(actual, expected, it)
assert.end()
})
test('medium gray', function (assert) {
var expected = '#808080'
var actual = hsl(0, 0, 50)
var it = '0% saturation, 50% luminosity should be medium gray'
assert.is(actual, expected, it)
assert.end()
})
test('hue - red', function (assert) {
var expected = '#ff0000'
var actual = hsl(0, 100, 50)
var it = '0deg should be red'
assert.is(actual, expected, it)
assert.end()
})
test('hue - blue', function (assert) {
var expected = '#0000ff'
var actual = hsl(240, 100, 50)
var it = '240deg should be blue'
assert.is(actual, expected, it)
assert.end()
})
test('hue - cyan', function (assert) {
var expected = '#00ffff'
var actual = hsl(180, 100, 50)
var it = '180deg should be cyan'
assert.is(actual, expected, it)
assert.end()
})
test('degree overflow', function (assert) {
var expected = hsl(1, 100, 50)
var actual = hsl(361, 100, 50)
var it = '361deg should be the same as 1deg'
assert.is(actual, expected, it)
assert.end()
})
test('degree underflow', function (assert) {
var expected = hsl(-1, 100, 50)
var actual = hsl(359, 100, 50)
var it = '-1deg should be the same as 359deg'
assert.is(actual, expected, it)
assert.end()
})
test('max constraint', function (assert) {
var expected = hsl(0, 101, 50)
var actual = hsl(0, 100, 50)
var it = '101% should be the same as 100%'
assert.is(actual, expected, it)
assert.end()
})
test('min constraint', function (assert) {
var expected = hsl(0, -1, 50)
var actual = hsl(0, 0, 50)
var it = '-1% should be the same as 0%'
assert.is(actual, expected, it)
assert.end()
})
@@ -0,0 +1,2 @@
node_modules
*.log
@@ -0,0 +1,6 @@
var hsl = require('./')
var hue = 133
var saturation = 40
var luminosity = 60
var hex = hsl(hue, saturation, luminosity)
console.log(hex) // #70c282
@@ -0,0 +1,66 @@
// In our case, there's only one dependency
var toRgb = require('hsl-to-rgb-for-reals')
var debug = require('debug')('hsl-to-hex')
// Typically all dependencies should be declared at the top of the file.
// Now let's define an API for our module, we're taking hue, saturation and luminosity values and outputting a CSS compatible hex string.
// Hue is in degrees, between 0 and 359. Since degrees a cyclical in nature, we'll support numbers greater than 359 or less than 0 by "spinning" them around until they fall within the 0 to 359 range.
// Saturation and luminosity are both percentages, we'll represent these percentages with whole numbers between 0 and 100. For these numbers we'll need to enforce a maximum and a minimum, anything below 0 will become 0, anything above 100 will become 100.
// Let's write some utility functions to handle this logic:
function max (val, n) {
debug('ensuring ' + val + ' is no more than ' + n)
return (val > n) ? n : val
}
function min (val, n) {
debug('ensuring ' + val + ' is no less than ' + n)
return (val < n) ? n : val
}
function cycle (val) {
debug('resolving ' + val + ' within the 0-359 range')
// for safety:
val = max(val, 1e7)
val = min(val, -1e7)
// cycle value:
while (val < 0) { val += 360 }
while (val > 359) { val -= 360 }
return val
}
// Now for the main piece, the `hsl` function:
function hsl (hue, saturation, luminosity) {
debug('calculating hex for hue: ' + hue + ' saturation: ' + saturation + ' luminosity: ' + luminosity)
// resolve degrees to 0 - 359 range
hue = cycle(hue)
// enforce constraints
saturation = min(max(saturation, 100), 0)
luminosity = min(max(luminosity, 100), 0)
// convert to 0 to 1 range used by hsl-to-rgb-for-reals
saturation /= 100
luminosity /= 100
// let hsl-to-rgb-for-reals do the hard work
var rgb = toRgb(hue, saturation, luminosity)
// convert each value in the returned RGB array
// to a 2 character hex value, join the array into
// a string, prefixed with a hash
return '#' + rgb
.map(function (n) {
return (256 + n).toString(16).substr(-2)
})
.join('')
}
// In order to make our code into a bona fide module we have to export it:
module.exports = hsl
@@ -0,0 +1,30 @@
{
"name": "@davidmarkclements/hsl-to-hex",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "npm run lint && tap --cov test",
"lint": "standard"
},
"author": "David Mark Clements",
"license": "ISC",
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/davidmarkclements/hsl-to-hex.git"
},
"bugs": {
"url": "https://github.com/davidmarkclements/hsl-to-hex/issues"
},
"homepage": "https://github.com/davidmarkclements/hsl-to-hex#readme",
"description": "Convert HSL colors to RGB colors in hex format.",
"dependencies": {
"hsl-to-rgb-for-reals": "^1.1.0"
},
"devDependencies": {
"standard": "^6.0.8",
"tap": "^5.7.0"
},
"directories": {
"test": "test"
}
}
@@ -0,0 +1,31 @@
# hsl-to-hex
Convert HSL colors to RGB colors in hex format.
## Install
```sh
npm install --save @davidmarkclements/hsl-to-hex
```
## API
```
require('hsl-to-hex') => Function
hsl(hue, saturation, luminosity)` => String
```
## Example
```js
var hsl = require('hsl-to-hex')
var hue = 133
var saturation = 40
var luminosity = 60
var hex = hsl(hue, saturation, luminosity)
console.log(hex) // #70c282
```
## License
ISC
@@ -0,0 +1,74 @@
var hsl = require('../')
var test = require('tap').test
test('pure white', function (assert) {
var expected = '#ffffff'
var actual = hsl(0, 100, 100)
var it = 'max saturation and luminosity should return pure white'
assert.is(actual, expected, it)
assert.end()
})
test('medium gray', function (assert) {
var expected = '#808080'
var actual = hsl(0, 0, 50)
var it = '0% saturation, 50% luminosity should be medium gray'
assert.is(actual, expected, it)
assert.end()
})
test('hue - red', function (assert) {
var expected = '#ff0000'
var actual = hsl(0, 100, 50)
var it = '0deg should be red'
assert.is(actual, expected, it)
assert.end()
})
test('hue - blue', function (assert) {
var expected = '#0000ff'
var actual = hsl(240, 100, 50)
var it = '240deg should be blue'
assert.is(actual, expected, it)
assert.end()
})
test('hue - cyan', function (assert) {
var expected = '#00ffff'
var actual = hsl(180, 100, 50)
var it = '180deg should be cyan'
assert.is(actual, expected, it)
assert.end()
})
test('degree overflow', function (assert) {
var expected = hsl(1, 100, 50)
var actual = hsl(361, 100, 50)
var it = '361deg should be the same as 1deg'
assert.is(actual, expected, it)
assert.end()
})
test('degree underflow', function (assert) {
var expected = hsl(-1, 100, 50)
var actual = hsl(359, 100, 50)
var it = '-1deg should be the same as 359deg'
assert.is(actual, expected, it)
assert.end()
})
test('max constraint', function (assert) {
var expected = hsl(0, 101, 50)
var actual = hsl(0, 100, 50)
var it = '101% should be the same as 100%'
assert.is(actual, expected, it)
assert.end()
})
test('min constraint', function (assert) {
var expected = hsl(0, -1, 50)
var actual = hsl(0, 0, 50)
var it = '-1% should be the same as 0%'
assert.is(actual, expected, it)
assert.end()
})
@@ -0,0 +1,2 @@
node_modules
*.log
@@ -0,0 +1,6 @@
var hsl = require('./')
var hue = 133
var saturation = 40
var luminosity = 60
var hex = hsl(hue, saturation, luminosity)
console.log(hex) // #70c282
@@ -0,0 +1,66 @@
// In our case, there's only one dependency
var toRgb = require('hsl-to-rgb-for-reals')
var debug = require('debug')('hsl-to-hex')
// Typically all dependencies should be declared at the top of the file.
// Now let's define an API for our module, we're taking hue, saturation and luminosity values and outputting a CSS compatible hex string.
// Hue is in degrees, between 0 and 359. Since degrees a cyclical in nature, we'll support numbers greater than 359 or less than 0 by "spinning" them around until they fall within the 0 to 359 range.
// Saturation and luminosity are both percentages, we'll represent these percentages with whole numbers between 0 and 100. For these numbers we'll need to enforce a maximum and a minimum, anything below 0 will become 0, anything above 100 will become 100.
// Let's write some utility functions to handle this logic:
function max (val, n) {
debug('ensuring ' + val + ' is no more than ' + n)
return (val > n) ? n : val
}
function min (val, n) {
debug('ensuring ' + val + ' is no less than ' + n)
return (val < n) ? n : val
}
function cycle (val) {
debug('resolving ' + val + ' within the 0-359 range')
// for safety:
val = max(val, 1e7)
val = min(val, -1e7)
// cycle value:
while (val < 0) { val += 360 }
while (val > 359) { val -= 360 }
return val
}
// Now for the main piece, the `hsl` function:
function hsl (hue, saturation, luminosity) {
debug('calculating hex for hue: ' + hue + ' saturation: ' + saturation + ' luminosity: ' + luminosity)
// resolve degrees to 0 - 359 range
hue = cycle(hue)
// enforce constraints
saturation = min(max(saturation, 100), 0)
luminosity = min(max(luminosity, 100), 0)
// convert to 0 to 1 range used by hsl-to-rgb-for-reals
saturation /= 100
luminosity /= 100
// let hsl-to-rgb-for-reals do the hard work
var rgb = toRgb(hue, saturation, luminosity)
// convert each value in the returned RGB array
// to a 2 character hex value, join the array into
// a string, prefixed with a hash
return '#' + rgb
.map(function (n) {
return (256 + n).toString(16).substr(-2)
})
.join('')
}
// In order to make our code into a bona fide module we have to export it:
module.exports = hsl
@@ -0,0 +1,32 @@
{
"name": "@davidmarkclements/hsl-to-hex",
"version": "1.0.1",
"main": "index.js",
"scripts": {
"prepublish": "npm ls && npm test",
"test": "npm run lint && tap --cov test",
"lint": "standard"
},
"author": "David Mark Clements",
"license": "ISC",
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/davidmarkclements/hsl-to-hex.git"
},
"bugs": {
"url": "https://github.com/davidmarkclements/hsl-to-hex/issues"
},
"homepage": "https://github.com/davidmarkclements/hsl-to-hex#readme",
"description": "Convert HSL colors to RGB colors in hex format.",
"dependencies": {
"debug": "^2.2.0",
"hsl-to-rgb-for-reals": "^1.1.0"
},
"devDependencies": {
"standard": "^6.0.8",
"tap": "^5.7.0"
},
"directories": {
"test": "test"
}
}
@@ -0,0 +1,31 @@
# hsl-to-hex
Convert HSL colors to RGB colors in hex format.
## Install
```sh
npm install --save @davidmarkclements/hsl-to-hex
```
## API
```
require('hsl-to-hex') => Function
hsl(hue, saturation, luminosity)` => String
```
## Example
```js
var hsl = require('hsl-to-hex')
var hue = 133
var saturation = 40
var luminosity = 60
var hex = hsl(hue, saturation, luminosity)
console.log(hex) // #70c282
```
## License
ISC
@@ -0,0 +1,74 @@
var hsl = require('../')
var test = require('tap').test
test('pure white', function (assert) {
var expected = '#ffffff'
var actual = hsl(0, 100, 100)
var it = 'max saturation and luminosity should return pure white'
assert.is(actual, expected, it)
assert.end()
})
test('medium gray', function (assert) {
var expected = '#808080'
var actual = hsl(0, 0, 50)
var it = '0% saturation, 50% luminosity should be medium gray'
assert.is(actual, expected, it)
assert.end()
})
test('hue - red', function (assert) {
var expected = '#ff0000'
var actual = hsl(0, 100, 50)
var it = '0deg should be red'
assert.is(actual, expected, it)
assert.end()
})
test('hue - blue', function (assert) {
var expected = '#0000ff'
var actual = hsl(240, 100, 50)
var it = '240deg should be blue'
assert.is(actual, expected, it)
assert.end()
})
test('hue - cyan', function (assert) {
var expected = '#00ffff'
var actual = hsl(180, 100, 50)
var it = '180deg should be cyan'
assert.is(actual, expected, it)
assert.end()
})
test('degree overflow', function (assert) {
var expected = hsl(1, 100, 50)
var actual = hsl(361, 100, 50)
var it = '361deg should be the same as 1deg'
assert.is(actual, expected, it)
assert.end()
})
test('degree underflow', function (assert) {
var expected = hsl(-1, 100, 50)
var actual = hsl(359, 100, 50)
var it = '-1deg should be the same as 359deg'
assert.is(actual, expected, it)
assert.end()
})
test('max constraint', function (assert) {
var expected = hsl(0, 101, 50)
var actual = hsl(0, 100, 50)
var it = '101% should be the same as 100%'
assert.is(actual, expected, it)
assert.end()
})
test('min constraint', function (assert) {
var expected = hsl(0, -1, 50)
var actual = hsl(0, 0, 50)
var it = '-1% should be the same as 0%'
assert.is(actual, expected, it)
assert.end()
})
@@ -0,0 +1,19 @@
{
"name": "hsl-to-hex",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "David Mark Clements",
"license": "ISC",
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/davidmarkclements/hsl-to-hex.git"
},
"bugs": {
"url": "https://github.com/davidmarkclements/hsl-to-hex/issues"
},
"homepage": "https://github.com/davidmarkclements/hsl-to-hex#readme",
"description": ""
}
@@ -0,0 +1,2 @@
node_modules
*.log
@@ -0,0 +1,19 @@
{
"name": "hsl-to-hex",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "David Mark Clements",
"license": "ISC",
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/davidmarkclements/hsl-to-hex.git"
},
"bugs": {
"url": "https://github.com/davidmarkclements/hsl-to-hex/issues"
},
"homepage": "https://github.com/davidmarkclements/hsl-to-hex#readme",
"description": ""
}
@@ -0,0 +1,2 @@
node_modules
*.log
@@ -0,0 +1,6 @@
var hsl = require('./')
var hue = 133
var saturation = 40
var luminosity = 60
var hex = hsl(hue, saturation, luminosity)
console.log(hex) // #70c282
@@ -0,0 +1,66 @@
// In our case, there's only one dependency
var toRgb = require('hsl-to-rgb-for-reals')
var debug = require('debug')('hsl-to-hex')
// Typically all dependencies should be declared at the top of the file.
// Now let's define an API for our module, we're taking hue, saturation and luminosity values and outputting a CSS compatible hex string.
// Hue is in degrees, between 0 and 359. Since degrees a cyclical in nature, we'll support numbers greater than 359 or less than 0 by "spinning" them around until they fall within the 0 to 359 range.
// Saturation and luminosity are both percentages, we'll represent these percentages with whole numbers between 0 and 100. For these numbers we'll need to enforce a maximum and a minimum, anything below 0 will become 0, anything above 100 will become 100.
// Let's write some utility functions to handle this logic:
function max (val, n) {
debug('ensuring ' + val + ' is no more than ' + n)
return (val > n) ? n : val
}
function min (val, n) {
debug('ensuring ' + val + ' is no less than ' + n)
return (val < n) ? n : val
}
function cycle (val) {
debug('resolving ' + val + ' within the 0-359 range')
// for safety:
val = max(val, 1e7)
val = min(val, -1e7)
// cycle value:
while (val < 0) { val += 360 }
while (val > 359) { val -= 360 }
return val
}
// Now for the main piece, the `hsl` function:
function hsl (hue, saturation, luminosity) {
debug('calculating hex for hue: ' + hue + ' saturation: ' + saturation + ' luminosity: ' + luminosity)
// resolve degrees to 0 - 359 range
hue = cycle(hue)
// enforce constraints
saturation = min(max(saturation, 100), 0)
luminosity = min(max(luminosity, 100), 0)
// convert to 0 to 1 range used by hsl-to-rgb-for-reals
saturation /= 100
luminosity /= 100
// let hsl-to-rgb-for-reals do the hard work
var rgb = toRgb(hue, saturation, luminosity)
// convert each value in the returned RGB array
// to a 2 character hex value, join the array into
// a string, prefixed with a hash
return '#' + rgb
.map(function (n) {
return (256 + n).toString(16).substr(-2)
})
.join('')
}
// In order to make our code into a bona fide module we have to export it:
module.exports = hsl
@@ -0,0 +1,33 @@
{
"name": "@ncb/hsl-to-hex",
"version": "1.0.1",
"main": "index.js",
"scripts": {
"check": "npm ls && npm test",
"test": "npm run lint && tap --cov test",
"lint": "standard",
"prepublish": "npm run check && ./publish-dep.sh"
},
"author": "David Mark Clements",
"license": "ISC",
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/davidmarkclements/hsl-to-hex.git"
},
"bugs": {
"url": "https://github.com/davidmarkclements/hsl-to-hex/issues"
},
"homepage": "https://github.com/davidmarkclements/hsl-to-hex#readme",
"description": "Convert HSL colors to RGB colors in hex format.",
"dependencies": {
"debug": "^2.2.0",
"hsl-to-rgb-for-reals": "^1.1.0"
},
"devDependencies": {
"standard": "^6.0.8",
"tap": "^5.7.0"
},
"directories": {
"test": "test"
}
}
@@ -0,0 +1,50 @@
#! /bin/sh
# 400 - Missing hash and/or peer_id
# 403 - Not allowed
# 413 - Too big
# 200 - Ok!
if echo $npm_config_argv | grep -q "install"; then
echo "Running at install step, skipping"
else
if ipfs &>/dev/null; then
echo "## Publishing dependency"
mv node_modules .node_modules 2>/dev/null
HASH=$(ipfs add . -r | tail -n 1 | cut -d ' ' -f 2)
mv .node_modules node_modules 2>/dev/null
echo "Published as $HASH"
PEER=$(ipfs id --format '<id>')
if [ -f ~/.stay/nodes.json ]; then
cat ~/.stay/nodes.json | jq -rc '.[]' | while read host; do
address="$host/api/pin/add/$HASH/$PEER"
status=$(curl -X POST --silent $address)
case "$status" in
"400") echo "$host - Application Error: Missing the hash and/or peer_id"
;;
"403") echo "$host - You do not have access to pinning at this node"
;;
"413") echo "$host - The module was too big to pin!"
;;
"200") echo "$host - Pinned!"
;;
*) echo "Weird status code $status for $host"
;;
esac
done
else
echo "You don't have any saved nodes in ~/.stay/nodes.json, skip pinning"
fi
else
echo "## Could not publish dependency to IPFS, doing the good'ol 'fetch from npm registry' way"
echo "Either 'ipfs' doesn't exists in PATH or you haven't run 'ipfs daemon' before running the command"
exit 0
fi
fi
@@ -0,0 +1,31 @@
# hsl-to-hex
Convert HSL colors to RGB colors in hex format.
## Install
```sh
npm install --save @davidmarkclements/hsl-to-hex
```
## API
```
require('hsl-to-hex') => Function
hsl(hue, saturation, luminosity)` => String
```
## Example
```js
var hsl = require('hsl-to-hex')
var hue = 133
var saturation = 40
var luminosity = 60
var hex = hsl(hue, saturation, luminosity)
console.log(hex) // #70c282
```
## License
ISC
@@ -0,0 +1,74 @@
var hsl = require('../')
var test = require('tap').test
test('pure white', function (assert) {
var expected = '#ffffff'
var actual = hsl(0, 100, 100)
var it = 'max saturation and luminosity should return pure white'
assert.is(actual, expected, it)
assert.end()
})
test('medium gray', function (assert) {
var expected = '#808080'
var actual = hsl(0, 0, 50)
var it = '0% saturation, 50% luminosity should be medium gray'
assert.is(actual, expected, it)
assert.end()
})
test('hue - red', function (assert) {
var expected = '#ff0000'
var actual = hsl(0, 100, 50)
var it = '0deg should be red'
assert.is(actual, expected, it)
assert.end()
})
test('hue - blue', function (assert) {
var expected = '#0000ff'
var actual = hsl(240, 100, 50)
var it = '240deg should be blue'
assert.is(actual, expected, it)
assert.end()
})
test('hue - cyan', function (assert) {
var expected = '#00ffff'
var actual = hsl(180, 100, 50)
var it = '180deg should be cyan'
assert.is(actual, expected, it)
assert.end()
})
test('degree overflow', function (assert) {
var expected = hsl(1, 100, 50)
var actual = hsl(361, 100, 50)
var it = '361deg should be the same as 1deg'
assert.is(actual, expected, it)
assert.end()
})
test('degree underflow', function (assert) {
var expected = hsl(-1, 100, 50)
var actual = hsl(359, 100, 50)
var it = '-1deg should be the same as 359deg'
assert.is(actual, expected, it)
assert.end()
})
test('max constraint', function (assert) {
var expected = hsl(0, 101, 50)
var actual = hsl(0, 100, 50)
var it = '101% should be the same as 100%'
assert.is(actual, expected, it)
assert.end()
})
test('min constraint', function (assert) {
var expected = hsl(0, -1, 50)
var actual = hsl(0, 0, 50)
var it = '-1% should be the same as 0%'
assert.is(actual, expected, it)
assert.end()
})
@@ -0,0 +1,59 @@
// In our case, there's only one dependency
var toRgb = require('hsl-to-rgb-for-reals')
// Typically all dependencies should be declared at the top of the file.
// Now let's define an API for our module, we're taking hue, saturation and luminosity values and outputting a CSS compatible hex string.
// Hue is in degrees, between 0 and 359. Since degrees a cyclical in nature, we'll support numbers greater than 359 or less than 0 by "spinning" them around until they fall within the 0 to 359 range.
// Saturation and luminosity are both percentages, we'll represent these percentages with whole numbers between 0 and 100. For these numbers we'll need to enforce a maximum and a minimum, anything below 0 will become 0, anything above 100 will become 100.
// Let's write some utility functions to handle this logic:
function max (val, n) {
return (val > n) ? n : val
}
function min (val, n) {
return (val < n) ? n : val
}
function cycle (val) {
// for safety:
val = max(val, 1e7)
val = min(val, -1e7)
// cycle value:
while (val < 0) { val += 360 }
while (val > 359) { val -= 360 }
return val
}
// Now for the main piece, the `hsl` function:
function hsl (hue, saturation, luminosity) {
// resolve degrees to 0 - 359 range
hue = cycle(hue)
// enforce constraints
saturation = min(max(saturation, 100), 0)
luminosity = min(max(luminosity, 100), 0)
// convert to 0 to 1 range used by hsl-to-rgb-for-reals
saturation /= 100
luminosity /= 100
// let hsl-to-rgb-for-reals do the hard work
var rgb = toRgb(hue, saturation, luminosity)
// convert each value in the returned RGB array
// to a 2 character hex value, join the array into
// a string, prefixed with a hash
return '#' + rgb
.map(function (n) {
return (256 + n).toString(16).substr(-2)
})
.join('')
}
// In order to make our code into a bona fide module we have to export it:
module.exports = hsl
@@ -0,0 +1,27 @@
{
"name": "hsl-to-hex",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "npm run lint && tap --cov test",
"lint": "standard"
},
"author": "David Mark Clements",
"license": "ISC",
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/davidmarkclements/hsl-to-hex.git"
},
"bugs": {
"url": "https://github.com/davidmarkclements/hsl-to-hex/issues"
},
"homepage": "https://github.com/davidmarkclements/hsl-to-hex#readme",
"description": "",
"dependencies": {
"hsl-to-rgb-for-reals": "^1.1.0"
},
"devDependencies": {
"standard": "^6.0.8",
"tap": "^5.7.0"
}
}
@@ -0,0 +1,74 @@
var hsl = require('../')
var test = require('tap').test
test('pure white', function (assert) {
var expected = '#ffffff'
var actual = hsl(0, 100, 100)
var it = 'max saturation and luminosity should return pure white'
assert.is(actual, expected, it)
assert.end()
})
test('medium gray', function (assert) {
var expected = '#808080'
var actual = hsl(0, 0, 50)
var it = '0% saturation, 50% luminosity should be medium gray'
assert.is(actual, expected, it)
assert.end()
})
test('hue - red', function (assert) {
var expected = '#ff0000'
var actual = hsl(0, 100, 50)
var it = '0deg should be red'
assert.is(actual, expected, it)
assert.end()
})
test('hue - blue', function (assert) {
var expected = '#0000ff'
var actual = hsl(240, 100, 50)
var it = '240deg should be blue'
assert.is(actual, expected, it)
assert.end()
})
test('hue - cyan', function (assert) {
var expected = '#00ffff'
var actual = hsl(180, 100, 50)
var it = '180deg should be cyan'
assert.is(actual, expected, it)
assert.end()
})
test('degree overflow', function (assert) {
var expected = hsl(1, 100, 50)
var actual = hsl(361, 100, 50)
var it = '361deg should be the same as 1deg'
assert.is(actual, expected, it)
assert.end()
})
test('degree underflow', function (assert) {
var expected = hsl(-1, 100, 50)
var actual = hsl(359, 100, 50)
var it = '-1deg should be the same as 359deg'
assert.is(actual, expected, it)
assert.end()
})
test('max constraint', function (assert) {
var expected = hsl(0, 101, 50)
var actual = hsl(0, 100, 50)
var it = '101% should be the same as 100%'
assert.is(actual, expected, it)
assert.end()
})
test('min constraint', function (assert) {
var expected = hsl(0, -1, 50)
var actual = hsl(0, 0, 50)
var it = '-1% should be the same as 0%'
assert.is(actual, expected, it)
assert.end()
})
@@ -0,0 +1,59 @@
// In our case, there's only one dependency
var toRgb = require('hsl-to-rgb-for-reals')
// Typically all dependencies should be declared at the top of the file.
// Now let's define an API for our module, we're taking hue, saturation and luminosity values and outputting a CSS compatible hex string.
// Hue is in degrees, between 0 and 359. Since degrees a cyclical in nature, we'll support numbers greater than 359 or less than 0 by "spinning" them around until they fall within the 0 to 359 range.
// Saturation and luminosity are both percentages, we'll represent these percentages with whole numbers between 0 and 100. For these numbers we'll need to enforce a maximum and a minimum, anything below 0 will become 0, anything above 100 will become 100.
// Let's write some utility functions to handle this logic:
function max (val, n) {
return (val > n) ? n : val
}
function min (val, n) {
return (val < n) ? n : val
}
function cycle (val) {
// for safety:
val = max(val, 1e7)
val = min(val, -1e7)
// cycle value:
while (val < 0) { val += 360 }
while (val > 359) { val -= 360 }
return val
}
// Now for the main piece, the `hsl` function:
function hsl (hue, saturation, luminosity) {
// resolve degrees to 0 - 359 range
hue = cycle(hue)
// enforce constraints
saturation = min(max(saturation, 100), 0)
luminosity = min(max(luminosity, 100), 0)
// convert to 0 to 1 range used by hsl-to-rgb-for-reals
saturation /= 100
luminosity /= 100
// let hsl-to-rgb-for-reals do the hard work
var rgb = toRgb(hue, saturation, luminosity)
// convert each value in the returned RGB array
// to a 2 character hex value, join the array into
// a string, prefixed with a hash
return '#' + rgb
.map(function (n) {
return (256 + n).toString(16).substr(-2)
})
.join('')
}
// In order to make our code into a bona fide module we have to export it:
module.exports = hsl
@@ -0,0 +1,26 @@
{
"name": "hsl-to-hex",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "npm run lint",
"lint": "standard"
},
"author": "David Mark Clements",
"license": "ISC",
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/davidmarkclements/hsl-to-hex.git"
},
"bugs": {
"url": "https://github.com/davidmarkclements/hsl-to-hex/issues"
},
"homepage": "https://github.com/davidmarkclements/hsl-to-hex#readme",
"description": "",
"dependencies": {
"hsl-to-rgb-for-reals": "^1.1.0"
},
"devDependencies": {
"standard": "^6.0.8"
}
}
@@ -0,0 +1,59 @@
// In our case, there's only one dependency
var toRgb = require('hsl-to-rgb-for-reals')
// Typically all dependencies should be declared at the top of the file.
// Now let's define an API for our module, we're taking hue, saturation and luminosity values and outputting a CSS compatible hex string.
// Hue is in degrees, between 0 and 359. Since degrees a cyclical in nature, we'll support numbers greater than 359 or less than 0 by "spinning" them around until they fall within the 0 to 359 range.
// Saturation and luminosity are both percentages, we'll represent these percentages with whole numbers between 0 and 100. For these numbers we'll need to enforce a maximum and a minimum, anything below 0 will become 0, anything above 100 will become 100.
// Let's write some utility functions to handle this logic:
function max (val, n) {
return (val > n) ? n : val
}
function min (val, n) {
return (val < n) ? n : val
}
function cycle (val) {
// for safety:
val = max(val, 1e7)
val = min(val, -1e7)
// cycle value:
while (val < 0) { val += 360 }
while (val > 359) { val -= 360 }
return val
}
// Now for the main piece, the `hsl` function:
function hsl (hue, saturation, luminosity) {
// resolve degrees to 0 - 359 range
hue = cycle(hue)
// enforce constraints
saturation = min(max(saturation, 100), 0)
luminosity = min(max(luminosity, 100), 0)
// convert to 0 to 1 range used by hsl-to-rgb-for-reals
saturation /= 100
luminosity /= 100
// let hsl-to-rgb-for-reals do the hard work
var rgb = toRgb(hue, saturation, luminosity)
// convert each value in the returned RGB array
// to a 2 character hex value, join the array into
// a string, prefixed with a hash
return '#' + rgb
.map(function (n) {
return (256 + n).toString(16).substr(-2)
})
.join('')
}
// In order to make our code into a bona fide module we have to export it:
module.exports = hsl
@@ -0,0 +1,27 @@
{
"name": "hsl-to-hex",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "npm run lint && tap --node-arg='--harmony-destructuring' --cov test",
"lint": "standard"
},
"author": "David Mark Clements",
"license": "ISC",
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/davidmarkclements/hsl-to-hex.git"
},
"bugs": {
"url": "https://github.com/davidmarkclements/hsl-to-hex/issues"
},
"homepage": "https://github.com/davidmarkclements/hsl-to-hex#readme",
"description": "",
"dependencies": {
"hsl-to-rgb-for-reals": "^1.1.0"
},
"devDependencies": {
"standard": "^6.0.8",
"tap": "^5.7.0"
}
}
@@ -0,0 +1,90 @@
const hsl = require('../')
const {test} = require('tap')
test('pure white', ({is, end}) => {
const expected = '#ffffff'
const actual = hsl(0, 100, 100)
const it = `
max saturation and luminosity should return pure white
`
is(actual, expected, it)
end()
})
test('medium gray', ({is, end}) => {
const expected = '#808080'
const actual = hsl(0, 0, 50)
const it = `
0% saturation, 50% luminosity should be medium gray
`
is(actual, expected, it)
end()
})
test('hue', ({is, end}) => {
{
const expected = '#ff0000'
const actual = hsl(0, 100, 50)
const it = `
0deg should be red
`
is(actual, expected, it)
}
{
const expected = '#0000ff'
const actual = hsl(240, 100, 50)
const it = `
240deg should be blue
`
is(actual, expected, it)
}
{
const expected = '#00ffff'
const actual = hsl(180, 100, 50)
const it = `
180deg should be cyan
`
is(actual, expected, it)
}
end()
})
test('degree overflow/underflow', ({is, end}) => {
{
const expected = hsl(1, 100, 50)
const actual = hsl(361, 100, 50)
const it = `
361deg should be the same as 1deg
`
is(actual, expected, it)
}
{
const expected = hsl(-1, 100, 50)
const actual = hsl(359, 100, 50)
const it = `
-1deg should be the same as 359deg
`
is(actual, expected, it)
}
end()
})
test('max constraint', ({is, end}) => {
{
const expected = hsl(0, 101, 50)
const actual = hsl(0, 100, 50)
const it = `
101% should be the same as 100%
`
is(actual, expected, it)
}
{
const expected = hsl(0, -1, 50)
const actual = hsl(0, 0, 50)
const it = `
-1% should be the same as 0%
`
is(actual, expected, it)
}
end()
})
@@ -0,0 +1,16 @@
'use strict'
const net = require('net')
const socket = net.connect(1337, 'localhost')
const name = process.argv[2] || 'Dave'
socket.write(name)
socket.on('data', (data) => {
console.log(data.toString())
})
socket.on('close', () => {
console.log('-> disconnected by server')
})
@@ -0,0 +1,14 @@
'use strict'
const net = require('net')
const socket = net.connect(1337)
const name = process.argv[2] || 'Dave'
socket.write(name)
socket.pipe(process.stdout)
socket.on('close', () => {
console.log('-> disconnected by server')
})
@@ -0,0 +1,3 @@
'use strict'
process.stdin.pipe(require('net').connect(1338)).pipe(process.stdout)
@@ -0,0 +1,3 @@
'use strict'
require('net').createServer((socket) => socket.pipe(socket)).listen(1338)
@@ -0,0 +1,13 @@
'use strict'
const net = require('net')
net.createServer((socket) => {
console.log('-> client connected')
socket.once('data', name => {
socket.write(`Hi ${name}!`)
})
socket.on('close', () => {
console.log('-> client disconnected')
})
}).listen(1337)
@@ -0,0 +1,13 @@
'use strict'
const net = require('net')
net.createServer((socket) => {
console.log('-> client connected')
socket.on('data', name => {
socket.write(`Hi ${name}!`)
})
socket.on('close', () => {
console.log('-> client disconnected')
})
}).listen(1337, 'localhost')
@@ -0,0 +1,13 @@
'use strict'
const dgram = require('dgram')
const socket = dgram.createSocket('udp4')
const name = process.argv[2] || 'Dave'
socket.bind(1400)
socket.send(name, 1339)
socket.on('message', (data) => {
console.log(data.toString())
})
@@ -0,0 +1,10 @@
'use strict'
const dgram = require('dgram')
const socket = dgram.createSocket('udp4')
socket.bind(1339)
socket.on('message', (name) => {
socket.send(`Hi ${name}!`, 1400)
})
@@ -0,0 +1,16 @@
'use strict'
const net = require('net')
const socket = net.connect('/tmp/my.socket')
const name = process.argv[2] || 'Dave'
socket.write(name)
socket.on('data', (data) => {
console.log(data.toString())
})
socket.on('close', () => {
console.log('-> disconnected by server')
})
@@ -0,0 +1,13 @@
'use strict'
const net = require('net')
net.createServer((socket) => {
console.log('-> client connected')
socket.on('data', name => {
socket.write(`Hi ${name}!`)
})
socket.on('close', () => {
console.log('-> client disconnected')
})
}).listen('/tmp/my.socket')
@@ -0,0 +1,17 @@
'use strict'
const fs = require('fs')
const exists = (file) => new Promise((resolve, reject) => {
fs.access(file, (err) => {
if (err) {
if (err.code !== 'ENOENT') { return reject(err) }
return resolve({file, exists: false})
}
resolve({file, exists: true})
})
})
exists(process.argv[2])
.then(({file, exists}) => console.log(`"${file}" does${exists ? '' : ' not'} exist`))
.catch(console.error)
@@ -0,0 +1,66 @@
'use strict'
const fs = require('fs')
const path = require('path')
const tableaux = require('tableaux')
const write = tableaux(
{name: 'Name', size: 20},
{name: 'Created', size: 30},
{name: 'Inode', size: 10},
{name: 'Mode', size: 8},
{name: 'Lnks', size: 4},
{name: 'Size', size: 6}
)
function print(dir) {
fs.readdirSync(dir)
.map((file) => ({file, dir}))
.map(toMeta)
.forEach(output)
write.newline()
}
function toMeta({file, dir}) {
const stats = fs.lstatSync(path.join(dir, file))
let {birthtime, ino, mode, nlink, size} = stats
birthtime = birthtime.toUTCString()
mode = mode.toString(8)
size += 'B'
return {
file,
dir,
info: [birthtime, ino, mode, nlink, size],
isDir: stats.isDirectory(),
isSymLink: stats.isSymbolicLink()
}
}
function output({file, dir, info, isDir, isSymLink}) {
if (isSymLink) {
outputSymlink(file, dir, info)
return
}
write(file, ...info)
if (!isDir) { return }
const p = path.join(dir, file)
write.arrow()
fs.readdirSync(p).forEach((f) => {
const stats = fs.lstatSync(path.join(p, f))
const style = stats.isDirectory() ? 'bold' : 'dim'
if (stats.isSymbolicLink()) { f = '\u001b[33m' + f + '\u001b[0m'}
write[style](f)
})
write.newline()
}
function outputSymlink(file, dir, info) {
write('\u001b[33m' + file + '\u001b[0m', ...info)
process.stdout.write('\u001b[33m')
write.arrow(4)
write.bold(fs.readlinkSync(path.join(dir, file)))
process.stdout.write('\u001b[0m')
write.newline()
}
print(process.argv[2] || '.')
@@ -0,0 +1 @@
my-subdir/my-subsubdir/too-deep

Some files were not shown because too many files have changed in this diff Show More