Skip to content

Commit

Permalink
Merge pull request #7 from marxjmoura/v0.1.0
Browse files Browse the repository at this point in the history
v0.1.0
  • Loading branch information
marxjmoura authored Dec 14, 2018
2 parents 056d610 + 3df49e0 commit 35c989c
Show file tree
Hide file tree
Showing 28 changed files with 342 additions and 122 deletions.
5 changes: 1 addition & 4 deletions .karma.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ module.exports = function (config) {
singleRun: true,
browsers: ['ChromeHeadless'],
frameworks: ['jasmine'],
files: [
{ pattern: 'src/tests/**/*.test.js', included: true },
{ pattern: 'src/tests/fixtures/*.txt', included: false, served: true }
],
files: ['src/tests/**/*.test.js'],
preprocessors: {
'src/**/*.js': ['webpack']
},
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## [v0.1.0](https://github.com/marxjmoura/inoutjs/releases/tag/v0.1.0) (2018-12-14)
### Features:
- `write()`
- `writeLine()`
- `save()`

## [v0.1.0-beta.3](https://github.com/marxjmoura/inoutjs/releases/tag/v0.1.0-beta.3) (2018-12-12)
### Breaking changes:
- Rename `contentType()` to `type()`
Expand Down
37 changes: 30 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,27 @@ Load InOut.js with an ES6 import:
import io from 'inoutjs'
```

InOut.js read and write files by exposing the method `io()`.
InOut.js read and write files by exposing the method `io()`:

```js
document.getElementById('file').onchange = function (e) {
var file = e.target.files[0];

io(file); // InOut.js file wrapper
var ioWrapper = io(file); // InOut.js file wrapper
};
```

Creating an empty file:

```js
var ioWrapper = io();
```

Creating from blob:

```js
var ioWrapper = io(blob);
```

### File info

`fullName()` get file name including extension
Expand Down Expand Up @@ -90,19 +101,31 @@ io(file).readLine(function (line, next) {
`write()` write content to file

```js
io({ fullName: 'foo.txt', type: 'text/plain' }).write('full content');
var ioWrapper = io().write('content');
```

`writeLine()` write line to file
`writeLine()` write content to file and break line

```js
io({ fullName: 'foo.txt', type: 'text/plain' }).writeLine('content');
var ioWrapper = io()
.writeLine('content')
.writeLine(); // Just break line
```

### Save file

`save()` download the file

```js
io({ fullName: 'foo.txt', type: 'text/plain' }).save();
io(file).save();
io(file).save('foo.xml'); // Override the file name
io(file).save('foo.xml', 'application/xml'); // Override the file name and type
```

`toFile()` get JavaScript File

```js
var file = io().toFile()
```

### Utility functions
Expand Down
2 changes: 1 addition & 1 deletion dist/inout.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,26 @@
</head>
<body>
<input id="file" type="file" />
<pre><code id="content"></code></pre>
<script src="../dist/inout.min.js"></script>
<script>
var content = document.getElementById('content');

document.getElementById('file').onchange = function (e) {
var file = e.target.files[0];

while (content.firstChild) {
content.removeChild(content.firstChild);
}

io(file).readLine(function (line, next) {
console.log(line === undefined ? 'EOF' : line);
if (line) {
var div = document.createElement('div')
div.innerText = line

content.appendChild(div)
}

next();
});
};
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "inoutjs",
"title": "InOut.js",
"version": "0.1.0-beta.3",
"version": "0.1.0",
"homepage": "https://github.com/marxjmoura/inoutjs",
"author": "Marx JMoura",
"license": "MIT",
Expand Down
37 changes: 37 additions & 0 deletions src/lib/file-factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import FileWrapper from './file'

class FileFactory {
constructor (file) {
this._file = file
}

create () {
if (this._file instanceof FileWrapper) {
return this._file.toFile()
} else if (this._file instanceof File) {
return this._file
} else if (this._file instanceof Blob) {
return this._fileFromBlob(this._file)
} else {
return this._emptyFile()
}
}

_fileFromBlob (blob) {
return new File([blob], 'untitled', {
type: blob.type,
lastModified: new Date()
})
}

_emptyFile () {
const blob = new Blob([], { type: 'text/plain' })

return new File([blob], 'untitled.txt', {
type: blob.type,
lastModified: new Date()
})
}
}

export default FileFactory
2 changes: 1 addition & 1 deletion src/lib/file-info.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fileExtension = /(?:\.([^.]+))?$/

class FileInfoWrapper {
constructor(file) {
constructor (file) {
this._file = file
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/file-reader.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const newline = /(\r?\n)/

class FileReaderWrapper {
constructor(file) {
constructor (file) {
this._file = file
this._offset = 0
this._chunkSize = 1024 * 4 // 4KB
Expand Down
28 changes: 28 additions & 0 deletions src/lib/file-save.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class FileSaveWrapper {
constructor (file) {
this._file = file
}

save (name, type) {
name = name || this._file.name
type = type || this._file.type

const blob = new Blob([this._file], { type: type })
const options = { type: blob.type, lastModified: this._file.lastModified }
const file = new File([blob], name, options)

this._download(file)
}

_download (file) {
const a = document.createElement('a')

a.href = URL.createObjectURL(file)
a.download = file.name

a.click()
a.remove()
}
}

export default FileSaveWrapper
26 changes: 9 additions & 17 deletions src/lib/file-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const sizeUnit = /B|KB|MB|GB/i
const exponentOf = { B: 0, KB: 1, MB: 2, GB: 3 }

class FileSizeWrapper {
constructor(file) {
constructor (file) {
this._file = file
}

Expand All @@ -12,28 +12,20 @@ class FileSizeWrapper {
return Number(this._file.size) / Math.pow(1024, exponentOf[unit])
}

greaterThan (maxSize, unit) {
const fileSize = this.calculate(unit)

return fileSize > maxSize
greaterThan (size, unit) {
return this.calculate(unit) > Number(size)
}

greaterOrEqual (maxSize, unit) {
const fileSize = this.calculate(unit)

return fileSize >= maxSize
greaterOrEqual (size, unit) {
return this.calculate(unit) >= Number(size)
}

lowerThan (minSize, unit) {
const fileSize = this.calculate(unit)

return fileSize < minSize
lowerThan (size, unit) {
return this.calculate(unit) < Number(size)
}

lowerOrEqual (minSize, unit) {
const fileSize = this.calculate(unit)

return fileSize <= minSize
lowerOrEqual (size, unit) {
return this.calculate(unit) <= Number(size)
}
}

Expand Down
Loading

0 comments on commit 35c989c

Please sign in to comment.