create-download-link
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -9,3 +9,3 @@ module.exports = function (opt) { | ||
var link = 'data:application/octet-stream,' + encodeURIComponent(text); | ||
var anchor = document.createElement("A"); | ||
var anchor = document.createElement('a'); | ||
anchor.setAttribute('href', link); | ||
@@ -12,0 +12,0 @@ anchor.setAttribute('download', filename); |
{ | ||
"name": "create-download-link", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Create link to download text as file", | ||
"main": "index.js", | ||
"dependencies": {}, | ||
"devDependencies": {}, | ||
"devDependencies": { | ||
"chai": "3.5.0", | ||
"jsdom": "8.3.1", | ||
"jsdom-global": "1.7.0", | ||
"mocha": "2.4.5" | ||
}, | ||
"scripts": { | ||
"test": "node test.js" | ||
"test": "mocha -r jsdom-global/register test.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/fhinkel/shell-emulator.git" | ||
"url": "git+https://github.com/fhinkel/create-download-link.git" | ||
}, | ||
@@ -18,5 +23,5 @@ "author": "", | ||
"bugs": { | ||
"url": "https://github.com/fhinkel/shell-emulator/issues" | ||
"url": "https://github.com/fhinkel/create-download-link/issues" | ||
}, | ||
"homepage": "https://github.com/fhinkel/shell-emulator#readme" | ||
"homepage": "https://github.com/fhinkel/create-download-link#readme" | ||
} |
# create-download-link | ||
Create a link to download a text file. | ||
Specify the content, filename, | ||
and title of the link via the options argument: | ||
*create-download-link* returns an anchor element. When the element is clicked, a file is | ||
downloaded. Specify the content of the file, the filename to be used, and | ||
the title of the anchor element. | ||
```javascript | ||
var data = opt.data; | ||
var title = opt.title; | ||
var filename = opt.filename; | ||
``` | ||
var createDownloadLink = require('create-download-link'); | ||
var opt = { | ||
data: 'Here is the content of the file', | ||
title: 'Click to download your file', | ||
filename: 'example.txt' | ||
}; | ||
var anchor = createDownloadLink(opt); | ||
``` | ||
Include the resulting ```anchor``` in your DOM. It will behave similar to the following link: | ||
[Click to download your file](https://raw.githubusercontent.com/fhinkel/create-download-link/master/example.txt). |
38
test.js
@@ -0,20 +1,30 @@ | ||
var assert = require('chai').assert; | ||
var createDownloadLink = require('./index.js'); | ||
var opt = { | ||
}; | ||
describe('When you invoke create-download-link,', function() { | ||
document = { | ||
createElement: function() { | ||
return { | ||
setAttribute: function() {}, | ||
appendChild: function() {} | ||
before(function () { | ||
var opt = { | ||
data: 'nice data', | ||
title: 'nice title', | ||
filename: 'filename.txt' | ||
}; | ||
}, | ||
createTextNode: function () { | ||
} | ||
}; | ||
this.anchor = createDownloadLink(opt); | ||
}); | ||
createDownloadLink(opt); | ||
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'); | ||
}); | ||
}); | ||
}); | ||
console.log('Done testing. All good'); | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
4013
7
40
2
0
20
4