npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

file-binders

v1.2.0

Published

The module includes 3 binder creators for defi.js which deal with files.

Downloads

48

Readme

file-binders npm version

The module includes 3 binder creators for defi.js which deal with files.

Usage

In browser environment these functions live at window.fileBinders object.

<script src="path/to/file-binders.min.js"></script>
const { file, dropFiles, dragOver } = fileBinders;
// ...
defi.bindNode(object, 'file', node, file());

// if you don't want to create variables
defi.bindNode(object, 'file', node, fileBinders.file());

The bundle can be downloaded at gh-pages branch


npm i file-binders
// import all binders
const { file, dropFiles, dragOver } = require('file-binders');

// import only needed binders
const file = require('file-binders/file');
const dropFiles = require('file-binders/dropfiles');
const dragOver = require('file-binders/dragover');

// ...

defi.bindNode(object, 'file', node, files());

file(readAs)

Returns a binder for file input. The binder allows not only to get basic data about a file, but also to read it without calling FileReader manually.

If readAs argument isn’t set, input value gets into the bound property after its changing (on "change" DOM event). If readAs is set, binder will read the file and transform it into the needed format (data URI, Blob...) and only after reading the file, the property will be changed.

The file (native File) or an array of files becomes the final value of the property in the presence of multiple attribute. At the same time the result of reading will get into the object of every file as readerResult property.

Arguments

readAs [optional] (string) - the argument value can be "arrayBufer", "binaryString", "dataURL", "text". The value depends on the presence of corresponding methods of the FileReader prototype.

Example

defi.bindNode(object, 'myKey', '.my-file', file('dataURL'));
// ... user changes input content
// choosing my-image.png from file system ...
defi.on(object, 'change:myKey', () => {
	console.log(object.myKey);
	// -->
	// File
	//	lastModified: 1383404384000
	//	lastModifiedDate: ...
	//	name: "my-image.png"
	//	readerResult: "data:image/png;base64,iVBO..."
	//		- the result of file reading
	//	size: 9378
	//	type: "image/png"
});

dropFiles(readAs)

Returns a binder which allows to drop files from a file manager to a given element. After bindNode is called HTML block gets needed DOM event listeners (eg, "dragover" and "drop"). When user drops files from file manager into the block, the property gets an array of dropped files as its value. As like file the binder accepts one optional argument called readAs which says how the files need to be read by FileReader: data URI, Blob... (the result of reading is placed in readerResult property of every file). If readAs isn't given, the property gets an array of files which wasn't read.

Arguments

readAs [optional] (string) - the argument value can be "arrayBuffer", "binaryString", "dataURL", "text". The value depends on the presence of corresponding methods of the FileReader prototype.

Example

defi.bindNode(object, 'myKey', '.drop-area', dropFiles('dataURL'));
// ... user drops one file called
// "my-image.png" into .drop-area block
defi.on(object, 'change:myKey', () => {
	console.log(object.myKey[0]);
	// -->
	// File
	//	lastModified: 1383404384000
	//	lastModifiedDate: ...
	//	name: "my-image.png"
	//	readerResult: "data:image/png;base64,iVBO..."
	//		- the result of file reading
	//	size: 9378
	//	type: "image/png"
});

dragOver()

Makes given property equal to true when something is dragged over a given node.

Arguments

none

Examples

defi.bindNode(object, 'myKey', '.my-node', dragOver());
defi.on(object, 'change:myKey', function() {
	if(object.myKey) {
		console.log('something is dragging over .my-node');
	} else {
		console.log('nothing is dragged over the node');
	}
});

Add "dragovered" class name when file (or another draggable object) is dragged over .my-node

defi.bindNode(object, 'myKey', '.my-node', dragOver());
defi.bindNode(object, 'myKey', '.my-node', className('dragovered'));