我已将此功能包装到 NPM 模块url-schemify 中:
var schemify = require('url-schemify');
var assert = require('assert');
// url-schemify adds default scheme (http) to the URLs that miss it
assert.equal(schemify('google.com'), 'http://google.com');
assert.equal(schemify('www.example.com'), 'http://www.example.com');
// default scheme could be configured through the options parameter
assert.equal(schemify('google.com', { scheme: 'https' }), 'https://google.com');
// { scheme: '' } will produce protocol-related URL
assert.equal(schemify('www.example.com', { scheme: '' }), '//www.example.com');
// url-schemify doesn't modify URLs that already have scheme or protocol-related ones:
assert.equal(schemify('http://google.com'), 'http://google.com');
assert.equal(schemify('https://www.example.com'), 'https://www.example.com');
assert.equal(schemify('ftp://example.com'), 'ftp://example.com');
assert.equal(schemify('//example.com'), '//example.com');