Skip to content

Commit

Permalink
Merge pull request #6 from marxjmoura/v0.1.0-beta.3
Browse files Browse the repository at this point in the history
v0.1.0 beta.3
  • Loading branch information
marxjmoura authored Dec 12, 2018
2 parents 8fac787 + 0e88e50 commit 056d610
Show file tree
Hide file tree
Showing 22 changed files with 756 additions and 164 deletions.
2 changes: 1 addition & 1 deletion karma.config.js → .karma.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = function (config) {
preprocessors: {
'src/**/*.js': ['webpack']
},
webpack: require('./webpack.config'),
webpack: require('./.webpack.config'),
reporters: ['progress', 'coverage'],
coverageReporter: {
dir: 'src/tests/coverage',
Expand Down
File renamed without changes.
22 changes: 16 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
## [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()`
### Features:
- `greaterThan()`
- `greaterOrEqual()`
- `lowerThan()`
- `lowerOrEqual()`
- `readChunk()`

## [v0.1.0-beta.2](https://github.com/marxjmoura/inoutjs/releases/tag/v0.1.0-beta.2) (2018-12-11)
### Features:
- Get file full name: `fullName()`
- Get file name: `name()`
- Get file extension: `ext()`
- Get file content type: `contentType()`
- Get file size: `size()`
- `fullName()`
- `name()`
- `ext()`
- `contentType()`
- `size()`

## [v0.1.0-beta.1](https://github.com/marxjmoura/inoutjs/releases/tag/v0.1.0-beta.1) (2018-12-08)
### Features:
- Read line by line: `readLine()`
- `readLine()`
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ var name = io(file).name(); // E.g. foo
var extension = io(file).ext(); // E.g. txt
```

`contentType()` get file content type
`type()` get file content type

```js
var contentType = io(file).contentType(); // E.g. text/plain
var type = io(file).type(); // E.g. text/plain
```

`size()` get file size
Expand Down Expand Up @@ -90,19 +90,19 @@ io(file).readLine(function (line, next) {
`write()` write content to file

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

`writeLine()` write line to file

```js
io({ fullName: 'foo.txt', contenType: 'text/plain' }).writeLine('content');
io({ fullName: 'foo.txt', type: 'text/plain' }).writeLine('content');
```

`save()` download the file

```js
io({ fullName: 'foo.txt', contenType: 'text/plain' }).save();
io({ fullName: 'foo.txt', type: 'text/plain' }).save();
```

### Utility functions
Expand All @@ -113,19 +113,19 @@ io({ fullName: 'foo.txt', contenType: 'text/plain' }).save();
io(file).greaterThan(100, 'KB'); // Options: B, KB, MB, GB
```

`greaterThan()` file size is greather or equal to option
`greaterOrEqual()` file size is greather or equal to option

```js
io(file).greaterOrEqual(100, 'KB'); // Options: B, KB, MB, GB
```

`greaterThan()` file size is lower than option
`lowerThan()` file size is lower than option

```js
io(file).lowerThan(100, 'KB'); // Options: B, KB, MB, GB
```

`greaterThan()` file size is lower or equal to option
`lowerOrEqual()` file size is lower or equal to option

```js
io(file).lowerOrEqual(100, 'KB'); // Options: B, KB, MB, GB
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.

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.

6 changes: 3 additions & 3 deletions 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.2",
"version": "0.1.0-beta.3",
"homepage": "https://github.com/marxjmoura/inoutjs",
"author": "Marx JMoura",
"license": "MIT",
Expand All @@ -13,8 +13,8 @@
"url": "git://github.com/marxjmoura/inoutjs.git"
},
"scripts": {
"build": "cross-env NODE_ENV=production webpack --output=./dist/inout.min.js",
"test": "cross-env NODE_ENV=test karma start karma.config.js"
"build": "cross-env NODE_ENV=production webpack --config .webpack.config.js --output=./dist/inout.min.js",
"test": "cross-env NODE_ENV=test karma start .karma.config.js"
},
"dependencies": {},
"devDependencies": {
Expand Down
8 changes: 4 additions & 4 deletions src/lib/file-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ class FileInfoWrapper {
this._file = file
}

contentType () {
return this._file.type
}

ext () {
return fileExtension.exec(this._file.name)[1]
}
Expand All @@ -20,6 +16,10 @@ class FileInfoWrapper {
name () {
return this._file.name.replace(fileExtension, '')
}

type () {
return this._file.type
}
}

export default FileInfoWrapper
52 changes: 43 additions & 9 deletions src/lib/file-reader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const newline = /(\r\n|\n|\r)/
const newline = /(\r?\n)/

class FileReaderWrapper {
constructor(file) {
Expand All @@ -9,17 +9,30 @@ class FileReaderWrapper {
this._decoder = new TextDecoder('utf-8')
}

readChunk (callback) {
if (typeof callback !== 'function') return

this._fileReader.onload = () => {
const buffer = new Uint8Array(this._fileReader.result)
const chunk = this._decoder.decode(buffer, { stream: true })

this._nextChunk(chunk, callback)
}

this._seek()
}

readLine (callback) {
if (typeof callback !== 'function') return

const chunk = { partialLine: '' }

this._fileReader.onload = () => {
const buffer = new Uint8Array(this._fileReader.result)
const decoded = this._decoder.decode(buffer, { stream: true })
const content = this._decoder.decode(buffer, { stream: true })

chunk.offset = 0
chunk.content = decoded.split(newline)
chunk.content = content.split(newline)

this._nextLine(chunk, callback)
}
Expand All @@ -31,6 +44,18 @@ class FileReaderWrapper {
return this._offset === this._file.size
}

_nextChunk (chunk, callback) {
const next = () => {
if (this._eof()) {
this._stop(callback)
} else {
this._seek()
}
}

callback(chunk, next)
}

_nextLine (chunk, callback) {
for (; chunk.offset < chunk.content.length && !newline.test(chunk.partialLine); chunk.offset++) {
chunk.partialLine += chunk.content[chunk.offset]
Expand All @@ -41,15 +66,20 @@ class FileReaderWrapper {

if (newline.test(chunk.partialLine)) {
chunk.partialLine = ''
} else if (eoc && !this._eof()) {
this._seek()
return
}

if (eoc && this._eof()) {
callback(undefined, () => {})
} else if (eoc) {
this._seek()
} else {
callback(line, () => this._nextLine(chunk, callback))
const next = () => {
if (eoc && this._eof()) {
this._stop(callback)
} else {
this._nextLine(chunk, callback)
}
}

callback(line, next)
}

_seek () {
Expand All @@ -67,6 +97,10 @@ class FileReaderWrapper {
this._fileReader.readAsArrayBuffer(slice)
this._offset = end
}

_stop (callback) {
callback(undefined, () => {})
}
}

export default FileReaderWrapper
24 changes: 24 additions & 0 deletions src/lib/file-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,30 @@ class FileSizeWrapper {

return Number(this._file.size) / Math.pow(1024, exponentOf[unit])
}

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

return fileSize > maxSize
}

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

return fileSize >= maxSize
}

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

return fileSize < minSize
}

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

return fileSize <= minSize
}
}

export default FileSizeWrapper
46 changes: 40 additions & 6 deletions src/lib/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ class FileWrapper {
this._size = new FileSizeWrapper(file)
}

contentType () {
if (this._isFileInstance()) {
return this._info.contentType()
}
}

ext () {
if (this._isFileInstance()) {
return this._info.ext()
Expand All @@ -28,12 +22,46 @@ class FileWrapper {
}
}

greaterThan (maxSize, unit) {
if (!this._isFileInstance()) return false
if (typeof maxSize !== 'number') return false

return this._size.greaterThan(maxSize, unit)
}

greaterOrEqual (maxSize, unit) {
if (!this._isFileInstance()) return false
if (typeof maxSize !== 'number') return false

return this._size.greaterOrEqual(maxSize, unit)
}

lowerThan (maxSize, unit) {
if (!this._isFileInstance()) return false
if (typeof maxSize !== 'number') return false

return this._size.lowerThan(maxSize, unit)
}

lowerOrEqual (maxSize, unit) {
if (!this._isFileInstance()) return false
if (typeof maxSize !== 'number') return false

return this._size.lowerOrEqual(maxSize, unit)
}

name () {
if (this._isFileInstance()) {
return this._info.name()
}
}

readChunk (callback) {
if (this._isFileInstance()) {
this._reader.readChunk(callback)
}
}

readLine (callback) {
if (this._isFileInstance()) {
this._reader.readLine(callback)
Expand All @@ -46,6 +74,12 @@ class FileWrapper {
}
}

type () {
if (this._isFileInstance()) {
return this._info.type()
}
}

_isFileInstance () {
return this._file instanceof File
}
Expand Down
Loading

0 comments on commit 056d610

Please sign in to comment.