Skip to content

Commit

Permalink
feat: initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnpma committed Apr 3, 2015
1 parent 484b823 commit ec61eb8
Show file tree
Hide file tree
Showing 11 changed files with 354 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# 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

[*.md]
trim_trailing_whitespace = false
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
*.log
.tmp
www/
node_modules/
bower_components/
credentials.json
res/
config.xml
platforms/
app/img/logo.png
.env
23 changes: 23 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
language: node_js
node_js:
- iojs
sudo: false
cache:
directories:
- node_modules
notifications:
email: false
slack:
secure: U8RoCCWTJpF//cWHk/0I1asptg3rVRFehRt2qeGz42Z5VYxh0g/Yr3DQBOGDOcRpbzfOBdFK4rOm9Nh5LLYiXiOEUGuGi1ZH+p7yWNkX+kZI9JzxktZjqKBgHQO2N/aBOGuYm/eBJvbFhZryDubYVf/V736f4N3KxL19JW4LsCY=
deploy:
provider: npm
email: [email protected]
skip_cleanup: true
on:
branch: master
repo: excellenteasy/android-icons
api_key:
secure: Ad3fh52neMPTDdft52/vLt9dkhvNKE2PZw3hpnh7SzXdg+GObqj4dnU0qN9pkma66dgBLyyzCjDY6S0Kzz+wZ46emu6jXd7I/zwY6D3mfHfoMOd2vVM366W+fmfYwp0YVDfScs6DYiBYcz7P9JA+riQQUfsXF9aiW7IRgDiPOwA=
env:
global:
secure: F3YYiOKoWR7zd5WF1HL/2YR6dqiXP6dLreaf5+crIV2MUpal0hm11/9Ub5ldlEMosQ3yndo3dIqzEQIK/IiaDX3rpajbfU3aEQs5Yt86TzeobZ7TjaRHthZzTmw5WxGmyM7Rd7yeJBiYRzItiwldrB4aRJYmcJQtC+tyWlVqSKA=
87 changes: 87 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# android-icons
[![Build Status](https://travis-ci.org/excellenteasy/android-icons.svg?branch=master)](https://travis-ci.org/excellenteasy/android-icons)
[![Dependency Status](https://david-dm.org/excellenteasy/android-icons.svg)](https://david-dm.org/excellenteasy/android-icons)
[![devDependency Status](https://david-dm.org/excellenteasy/android-icons/dev-status.svg)](https://david-dm.org/excellenteasy/android-icons#info=devDependencies)

> Get android icon file names and dimensions (width)
The default icon file names (as used by [cordova](https://cordova.apache.org/docs/en/4.0.0/config_ref_images.md.html#Icons%20and%20Splash%20Screens)) and required sizes for android are listed in a [JSON file](icons.json). This information is useful, for example, when you want to generate icons with the required sizes or to create a [`config.xml`](http://docs.phonegap.com/en/3.5.0/config_ref_images.md.html) file for a PhoneGap/Cordova project or if you just need to create the icons for your android project from one source icon.
This information also exists for [iOS icons](https://github.com/excellenteasy/ios-icons).


## Install

```sh
$ npm install --save android-icons
```


## Usage

```js
var icons = require('android-icons');

icons();
//=> [{ "name": "mdpi.png", "width": 48 }, { "name": "hdpi.png", "width": 72 }, ... ]

icons({size: '48'})
//=> {name: 'mdpi.png', width: 48}

icons({size: 'xhdpi'})
//=> {name: 'xhdpi.png', width: 96}
```


## API

### icons()

Returns an array of icons, each icon being represented by an object with `name` and `width` properties.

> Notice that icons are always squares, so no `height` property is provided.
### icons(options)
#### options

Only option for now is `size`, which can be either a `Number` or `String` value. If it is a `Number`, it represents the width in pixels. If it is a `String`, you can use `"mdpi"`, `"hdpi"` etc. to refer to a certain size or the complete file name, e.g. `xhdpi.png`.

Returns icon object for that size or `null`.

For example:

```js
icons({size: 'xhdpi'})
//=> {name: 'xhdpi.png', width: 96}
```


## CLI
> android-icons logs to stdout in comma-separated values format (csv) by default so you can easy pipe to other commands in UNIX systems.
```sh
$ npm install --global android-icons
```

```sh
$ android-icons --help
Get android icon file names and dimensions (width)

Use `--format json` to set output to JSON.
Get specifc icon by size or name by using `--size`.

Examples:
$ android-icons --size 48
[email protected],80

$ android-icons --size 80 --format json
{"name":"[email protected]","width":80}

$ android-icons --size small
icon-small.png,29
```

## Semantic Releases
This module is being [semantically released](https://github.com/boennemann/semantic-release). You can safely use `"^1.0.0"` in your `package.json`.

## License
MIT © [David Pfahler](http://excellenteasy.com)
53 changes: 53 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env node
'use strict'
var abbrev = require('abbrev')
var argv = require('minimist')(process.argv.slice(2), abbrev('help', 'version', 'size', 'format'))
var pkg = require('./package.json')
var icons = require('./')

function help () {
console.log([
pkg.description,
'',
'Use `--format json` to set output to JSON.',
'Get specifc icon by size or name by using `--size`.',
'',
'Examples:',
' $ android-icons --size 48',
' mdpi.png,48',
'',
' $ android-icons --size 48 --format json',
' {"name":"mdpi.png","width":48}',
'',
' $ android-icons --size xhdpi',
' xhdpi.png,96'
].join('\n'))
}

function formatLog (icons, argv) {
var format = (argv.format || 'csv').toLowerCase()
if (format === 'json') {
return JSON.stringify(icons)
}
if (!Array.isArray(icons)) {
icons = [icons]
}
return icons.map(function (icon) {
return icon.name + ',' + icon.width
}).join('\n')
}

function cli () {
if (argv.help) return help()

if (argv.version) return console.log(pkg.version)

var options = {
size: argv.size || argv.s
}

var output = icons(options)
if (output) console.log(formatLog(output, argv))
}

cli()
16 changes: 16 additions & 0 deletions icons.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[{
"name": "mdpi.png",
"width": 48
}, {
"name": "hdpi.png",
"width": 72
}, {
"name": "xhdpi.png",
"width": 96
}, {
"name": "xxhdpi.png",
"width": 144
}, {
"name": "xxxhdpi.png",
"width": 192
}]
43 changes: 43 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict'
var icons = require('./icons.json')
var idRegEx = /^(.*)\.png$/
var widths = icons.map(function (icon) {
return icon.width
})
var ids = icons.map(function (icon) {
return icon.name.match(idRegEx)[1]
})

function getIconForSize (size) {
var width = getWidthForSize(size)

if (!width) {
return null
}

return icons[widths.indexOf(width)] || null
}

function getWidthForSize (size) {
if (typeof size === 'number') return size

var width = Number(size)
if (!isNaN(width)) return width

width = widths[ids.indexOf(size)]
if (width) return width

return widths[ids.indexOf(size.match(idRegEx)[1])]
}

module.exports = function (options) {
options = options || {}
var size = options.size
if (!size) return icons

return getIconForSize(size)
}

module.exports.icons = icons
module.exports.getIconForSize = getIconForSize
module.exports.getWidthForSize = getWidthForSize
21 changes: 21 additions & 0 deletions license
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) David Pfahler <[email protected]> (excellenteasy.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
48 changes: 48 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "android-icons",
"version": "0.0.0-semantically-released",
"description": "Get android icon file names and dimensions (width)",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/excellenteasy/android-icons"
},
"bin": {
"android-icons": "cli.js"
},
"author": {
"name": "David Pfahler",
"email": "[email protected]",
"url": "http://excellenteasy.com"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"test": "standard && node test.js",
"prepublish": "semantic-release pre",
"postpublish": "semantic-release post"
},
"files": [
"index.js",
"cli.js",
"icons.json"
],
"keywords": [
"cli",
"bin",
"icon",
"android",
"cordova",
"phonegap"
],
"dependencies": {
"abbrev": "^1.0.5",
"minimist": "^1.1.0"
},
"devDependencies": {
"semantic-release": "^3.0.4",
"standard": "^3.3.2",
"tape": "^3.0.3"
}
}
38 changes: 38 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict'
var test = require('tape')
var androidIcons = require('./')

test('returns all icons in array', function (t) {
t.plan(2)
var icons = androidIcons()
t.ok(Array.isArray(icons), 'returned an array')
t.equal(icons.length, 5, '5 icons returned')
})

test('returns icon for size 48 as Number', function (t) {
t.plan(2)
var icon = androidIcons({size: 48})
t.ok(icon.name === 'mdpi.png', 'icon name correct')
t.ok(icon.width === 48, 'icon width correct')
})

test('returns icon for size 48 as String', function (t) {
t.plan(2)
var icon = androidIcons({size: '48'})
t.ok(icon.name === 'mdpi.png', 'icon name correct')
t.ok(icon.width === 48, 'icon width correct')
})

test('returns icon for size 96', function (t) {
t.plan(2)
var icon = androidIcons({size: 96})
t.ok(icon.name === 'xhdpi.png', 'icon name correct')
t.ok(icon.width === 96, 'icon width correct')
})

test('returns icon for size xhdpi', function (t) {
t.plan(2)
var icon = androidIcons({size: 'xhdpi'})
t.ok(icon.name === 'xhdpi.png', 'icon name correct')
t.ok(icon.width === 96, 'icon width correct')
})

0 comments on commit ec61eb8

Please sign in to comment.