Socket
Socket
Sign inDemoInstall

create-download-link

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

create-download-link - npm Package Compare versions

Comparing version 1.0.4 to 1.1.0

47

index.js

@@ -1,26 +0,33 @@

module.exports = function (opt) {
module.exports = function(opt) {
var throwError = function(name) {
throw new Error('No ' + name + ' provided to create-download-link.');
};
var data = opt.data;
var title = opt.title;
var filename = opt.filename;
var data = opt.data || throwError('data');
var title = opt.title || throwError('title');
var filename = opt.filename || throwError('filename');
// creates
// <a href="data:application/octet-stream,data" download="filename">title</a>
var anchor = document.createElement('a');
var createDownloadableFileLink = function (data, filename) {
var link = 'data:application/octet-stream,' + encodeURIComponent(data);
var anchor = document.createElement('a');
anchor.setAttribute('href', link);
anchor.setAttribute('download', filename);
return anchor;
};
var attachDataAsOctetStream = function(data) {
var link = 'data:application/octet-stream,' + encodeURIComponent(data);
anchor.setAttribute('href', link);
};
var setTitleTo = function(anchor, title) {
anchor.appendChild(document.createTextNode(title));
return anchor;
};
var setFilename = function(filename) {
anchor.setAttribute('download', filename);
};
var anchor = createDownloadableFileLink(data, filename);
anchor = setTitleTo(anchor, title);
return anchor;
var setTitle = function(title) {
anchor.appendChild(document.createTextNode(title));
};
// creates
// <a href="data:application/octet-stream,DATA" download="FILENAME">TITLE</a>
attachDataAsOctetStream(data);
setFilename(filename);
setTitle(title);
return anchor;
};
{
"name": "create-download-link",
"version": "1.0.4",
"version": "1.1.0",
"description": "Create link to download text as file",

@@ -10,2 +10,4 @@ "main": "index.js",

"coveralls": "^2.11.9",
"eslint": "^2.7.0",
"eslint-config-google": "^0.5.0",
"istanbul": "^0.4.3",

@@ -21,4 +23,8 @@ "jsdom": "8.3.1",

"test": "mocha -r jsdom-global/register test.js",
"coverage": "istanbul cover _mocha -- -r jsdom-global/register test.js"
"coverage": "istanbul cover _mocha -- -r jsdom-global/register test.js",
"lint": "eslint --fix --env browser --env mocha ."
},
"eslintConfig": {
"extends": "google"
},
"repository": {

@@ -25,0 +31,0 @@ "type": "git",

@@ -10,3 +10,3 @@ [![Build Status](https://travis-ci.org/fhinkel/create-download-link.svg?branch=master)](https://travis-ci.org/fhinkel/create-download-link)

```html
<a href="data:application/octet-stream,data" download="filename">title</a>
<a href="data:application/octet-stream,DATA" download="FILENAME">TITLE</a>
```

@@ -20,3 +20,3 @@

When the element is clicked, a file is
downloaded and saved as *filename*.
downloaded and saved as *FILENAME*.

@@ -23,0 +23,0 @@ ## Usage

var assert = require('chai').assert;
var createDownloadLink = require('./index.js');
describe('When you invoke create-download-link,', function () {
describe('When you invoke create-download-link,', function() {
before(function() {
var opt = {
data: 'nice data',
title: 'nice title',
filename: 'filename.txt'
};
this.anchor = createDownloadLink(opt);
});
before(function () {
var opt = {
data: 'nice data',
title: 'nice title',
filename: 'filename.txt'
};
this.anchor = createDownloadLink(opt);
describe('the link', function() {
it('should be of type anchor', function() {
assert.equal(this.anchor.nodeName, 'A');
});
describe('the link', function () {
it('should be of type anchor', function () {
assert.equal(this.anchor.nodeName, 'A');
});
it('should have the correct attributes', function () {
assert.equal(this.anchor.getAttribute('href'), 'data:application/octet-stream,nice%20data');
assert.equal(this.anchor.getAttribute('download'), 'filename.txt');
});
it('should have a title', function () {
assert.equal(this.anchor.childNodes.length, 1);
assert.equal(this.anchor.firstChild.nodeValue, 'nice title');
});
it('should have the correct attributes', function() {
assert.equal(this.anchor.getAttribute('href'),
'data:application/octet-stream,nice%20data');
assert.equal(this.anchor.getAttribute('download'),
'filename.txt');
});
describe('creation', function () {
it('should fail if data is missing', function () {
});
it('should fail if data is missing', function () {
});
it('should fail if data is missing', function () {
});
it('should have a title', function() {
assert.equal(this.anchor.childNodes.length, 1);
assert.equal(this.anchor.firstChild.nodeValue, 'nice title');
});
});
describe('creation', function() {
it('should fail if data is missing', function() {
var opt = {
title: 'nice title',
filename: 'filename.txt'
};
assert.throw(function() {
createDownloadLink(opt);
}, Error, 'No data provided to create-download-link');
});
it('should fail if title is missing', function() {
var opt = {
data: 'nice data',
filename: 'filename.txt'
};
assert.throw(function() {
createDownloadLink(opt);
}, Error, 'No title provided to create-download-link');
});
it('should fail if filename is missing', function() {
var opt = {
data: 'nice data',
title: 'nice title'
};
assert.throw(function() {
createDownloadLink(opt);
}, Error, 'No filename provided to create-download-link');
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc