Skip to content

Commit

Permalink
string format in sizebytes, file limit
Browse files Browse the repository at this point in the history
  • Loading branch information
tasos committed May 21, 2021
1 parent 4b05c70 commit cdb357e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Tiny cypress command to upload fast large files to an HTML input type="file".
The file that will be uploaded is empty. Multiple attribute is not supported {yet}.


# Installation
```bash
npm i dummyattachfile
Expand All @@ -19,7 +18,10 @@ import 'dummyattachfile';
```javascript
cy.get('input').dummyAttachFile(<bytes>, <filename>, <filetype>)

// <bytes> Number | Optional | Default value: 1024
// <bytes> Number or String | Optional | Default value: 1024
// Number expressed in bytes
// Valid String Format: d+K, d+M eg. 666K, 500M etc.
// Current file limit: 1GB
// <filename> String | Optional | Default value: 'whatever.txt'
// <filetype> String | Optional | Default value: 'text/plain'

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dummyattachfile",
"version": "0.0.12",
"version": "0.0.13",
"description": "dummy uploading a file for cypress tests",
"main": "dist/bundle.js",
"source": "src/index.js",
Expand Down
36 changes: 36 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,49 @@
export default function dummyAttachFile(subject, sizeBytes = 1024, fileName = 'whatever.txt', fileType = 'text/plain') {

// if bytes do not respect the FILE_LIMIT
// force it to FILE_LIMIT
const FILE_LIMIT = 1047527424

// if arguments do not respect their type
// convert them to defaults

if (typeof sizeBytes !== 'number') {
if (typeof sizeBytes === 'string') {
const re = /^\d+[MK]{1}$/
if (re.test(sizeBytes)) {
const MB = sizeBytes.charAt(sizeBytes.length-1)
const parsed = parseInt(sizeBytes.substring(0, sizeBytes.length-1), 10)
// is it possible isNaN to be true??
// since regexp has handled the format?
if (isNaN(parsed)){
sizeBytes = 1024
}
else {
// 'K' Kilobytes case
sizeBytes = parsed*1024
// 'M' Megabytes case
if ( MB === 'M') {
sizeBytes *= 1024
}

}
} else {
sizeBytes = 1024
}
}
else {
sizeBytes = 1024
}
}

if (sizeBytes > FILE_LIMIT) {
sizeBytes = FILE_LIMIT
}

if (typeof fileName !== 'string') {
fileName = 'whatever.txt'
}

if (typeof fileType !== 'string') {
fileType = 'text/plain'
}
Expand Down

0 comments on commit cdb357e

Please sign in to comment.