Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hughsk committed Jul 20, 2013
0 parents commit 35a51e4
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
18 changes: 18 additions & 0 deletions LICENSE.md
@@ -0,0 +1,18 @@
This software is released under the MIT license:

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.
51 changes: 51 additions & 0 deletions README.md
@@ -0,0 +1,51 @@
# file-size-tree [![unstable](https://rawgithub.com/hughsk/stability-badges/master/dist/unstable.svg)](http://github.com/hughsk/stability-badges) #

Take a list of file paths in Node, and get back an object matching d3's
[hierarchy layout](https://github.com/mbostock/d3/wiki/Hierarchy-Layout)
format. Great for making easy
[treemaps](http://bl.ocks.org/mbostock/4063582) and the like.

## Installation ##

``` bash
npm install file-size-tree
```

## Usage ##

### `require('file-size-tree')(files)` ###

Takes an array of filenames and returns an object in d3's hierarchy layout
format. For example, this:

``` javascript
var fileTree = require('file-size-tree')

fileTree([
__dirname + '/project/src/index.js'
, __dirname + '/project/src/README.md'
, __dirname + '/project/src/package.json'
, __dirname + '/LICENSE'
])
```

Should result in this:

``` json
[{
"name": "project",
"children": [
{
"name": "src",
"children": [
{"name": "index.js", "size": 3938},
{"name": "README.md", "size": 3812},
{"name": "package.json", "size": 743}
]
}
]
}, {
"name": "LICENSE",
"size": 526
}]
```
46 changes: 46 additions & 0 deletions index.js
@@ -0,0 +1,46 @@
var commondir = require('commondir')
var path = require('path')
var flat = require('flat')
var fs = require('fs')

module.exports = tree

function tree(filenames) {
filenames = filenames.map(resolve)

var top = commondir(filenames)
var dirs = {}

var object = filenames.reduce(function(memo, name) {
var key = path.relative(top, name)
memo[key] = fs.statSync(name).size
return memo
}, {})

object = flat.unflatten(object, { delimiter: '/' })
object = reformat(object)

return object
}

function reformat(object) {
if (typeof object !== 'object') return object

var entries = []
var result
var entry
for (var key in object) {
entry = reformat(object[key])
if (typeof entry === 'number') {
entries.push({ name: key, size: entry })
} else {
entries.push({ name: key, children: entry })
}
}

return entries
}

function resolve(file) {
return path.resolve(file)
}
25 changes: 25 additions & 0 deletions package.json
@@ -0,0 +1,25 @@
{
"name": "file-size-tree",
"version": "0.0.0",
"description": "Take a list of file paths in Node, and get back an object matching d3's hierarchy layout format",
"main": "index.js",
"repository": {
"type": "git",
"url": "git://github.com/hughsk/file-size-tree.git"
},
"author": "Hugh Kennedy <hughskennedy@gmail.com> (http://hughskennedy.com/)",
"license": "MIT",
"dependencies": {
"flat": "~1.0.0",
"commondir": "0.0.1"
},
"readmeFilename": "README.md",
"devDependencies": {},
"keywords": [
"file",
"size",
"data",
"hierarchical",
"d3"
]
}

0 comments on commit 35a51e4

Please sign in to comment.