https://github.com/cdcgov/fileto
A little library to work with Javascript File objects
Science Score: 13.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
○.zenodo.json file
-
○DOI references
-
○Academic publication links
-
○Academic email domains
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (10.6%) to scientific vocabulary
Repository
A little library to work with Javascript File objects
Basic Info
- Host: GitHub
- Owner: CDCgov
- License: apache-2.0
- Language: JavaScript
- Default Branch: master
- Homepage: https://github.com/CDCgov/fileto#fileto
- Size: 16.6 KB
Statistics
- Stars: 1
- Watchers: 7
- Forks: 4
- Open Issues: 0
- Releases: 0
Metadata Files
README.md
fileto
A little library to work with Javascript File objects.
This repository was created for use by CDC programs to collaborate on public health surveillance related projects in support of the CDC Surveillance Strategy. Github is not hosted by the CDC, but is used by CDC and its partners to share information and collaborate on software.
impetus
Suppose we have an HTML file input, like so:
html
<input type="file" id="fileinput" />
Now, let's get the corresponding File object in Javascript.
javascript
var file = document.getElementById("fileinput");
What can we do with this? We can use its metadata, or we can read it with a FileReader, and not a lot more than that. More importantly, if we change the contents of the input, the file object goes *poof!* and ceases to be. In a complex, file-oriented web application, this may be extremely undesirable. So, to solve this, we can stash away the contents of the file. fileto is a little library to make that a little bit easier.
functions
Suppose we load a textfile called test.txt into the above file input. Its contents are:
text
foobar
To get the actual File object, we'll need to listen to the change event:
javascript
file.addEventListener("change", function(){ /* ...Our code here... */ });
Then fileto can do the following:
fileto.object(<file>) - Unwraps a File object.
javascript
fileto.object(this.files[0]);
/* {
"name":"test.txt",
"lastModified":1544812259347,
"size":7,
"type":"text/plain"
} */
Note that the return is a plain Object, rather than a File object. This is useful if you need to track a File's metadata after you may no longer have access to the File (e.g., the File Input has already been reused).
fileto.string(<file>) - Unwraps a File object and returns its contents as a JSON string. Literally just a wrapper for JSON.stringify(fileto.object(<file>))
javascript
fileto.string(this.files[0]);
/* '{"name":"test.txt","lastModified":1544812259347,"size":7,"type":"text/plain"}' */
fileto.callback(<file>, callback, readAs) - Unwraps a File Object, reads its contents, and executes a callback. The unwrapped file Object will be passed to the callback, and its contents will be in file.contents.
javascript
fileto.callback(this.files[0], function(output){
console.log(output);
});
/* {
contents: "foobar"
lastModified: 1544812259347
name: "test.txt"
size: 7
type: "text/plain"
} */
By default, readAs is Text, but can be set to ArrayBuffer, BinaryString, DataURL, or Text.
fileto.promise(<file>, readAs) - Returns a promise to unwrap a File Object, read the file's contents, and resolve the promise with the unwrapped Object. The contents will be in response.contents.
javascript
fileto.promise(this.files[0]).then(function(output){
console.log(output);
});
/* {
contents: "foobar"
lastModified: 1544812259347
name: "test.txt"
size: 7
type: "text/plain"
} */
If Promises aren't available, returns an object with a then function that executes a callback as though it were a promise, and returns an object with a catch attribute pointing to a null function. Basically, it's a hack to make this thing work under 90% of the circumstances that people use promises for. Note that while this does not enable chaining. If you need Promises for users of terrible browsers, Polyfill them.
To see each of these in action, check out the demo.
Public Domain
This repository constitutes a work of the United States Government and is not subject to domestic copyright protection under 17 USC § 105. This repository is in the public domain within the United States, and copyright and related rights in the work worldwide are waived through the CC0 1.0 Universal public domain dedication. All contributions to this repository will be released under the CC0 dedication. By submitting a pull request you are agreeing to comply with this waiver of copyright interest.
License
The repository utilizes code licensed under the terms of the Apache Software License and therefore is licensed under ASL v2 or later.
This source code in this repository is free: you can redistribute it and/or modify it under the terms of the Apache Software License version 2, or (at your option) any later version.
This source code in this repository is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache Software License for more details.
You should have received a copy of the Apache Software License along with this program. If not, see http://www.apache.org/licenses/LICENSE-2.0.html
The source code forked from other open source projects will inherit its license.
Privacy
This repository contains only non-sensitive, publicly available data and information. All material and community participation is covered by the Surveillance Platform Disclaimer and Code of Conduct. For more information about CDC's privacy policy, please visit http://www.cdc.gov/privacy.html.
Contributing
Anyone is encouraged to contribute to the repository by forking and submitting a pull request. (If you are new to GitHub, you might start with a basic tutorial.) By contributing to this project, you grant a world-wide, royalty-free, perpetual, irrevocable, non-exclusive, transferable license to all users under the terms of the Apache Software License v2 or later.
All comments, messages, pull requests, and other submissions received through CDC including this GitHub page are subject to the Presidential Records Act and may be archived. Learn more at http://www.cdc.gov/other/privacy.html.
Records
This repository is not a source of government records, but is a copy to increase collaboration and collaborative potential. All government records will be published through the CDC web site.
Notices
Please refer to CDC's Template Repository for more information about contributing to this repository, public domain notices and disclaimers, and code of conduct.
Owner
- Name: Centers for Disease Control and Prevention
- Login: CDCgov
- Kind: organization
- Email: data@cdc.gov
- Location: Atlanta, GA
- Website: http://open.cdc.gov/
- Twitter: CDCgov
- Repositories: 114
- Profile: https://github.com/CDCgov
CDC's collaborative software projects to protect America from health, safety, and security threats, both foreign and in the U.S.
GitHub Events
Total
- Fork event: 3
Last Year
- Fork event: 3
Issues and Pull Requests
Last synced: over 1 year ago
All Time
- Total issues: 0
- Total pull requests: 0
- Average time to close issues: N/A
- Average time to close pull requests: N/A
- Total issue authors: 0
- Total pull request authors: 0
- Average comments per issue: 0
- Average comments per pull request: 0
- Merged pull requests: 0
- Bot issues: 0
- Bot pull requests: 0
Past Year
- Issues: 0
- Pull requests: 0
- Average time to close issues: N/A
- Average time to close pull requests: N/A
- Issue authors: 0
- Pull request authors: 0
- Average comments per issue: 0
- Average comments per pull request: 0
- Merged pull requests: 0
- Bot issues: 0
- Bot pull requests: 0