random-path
Advanced tools
Comparing version 0.1.2 to 1.0.0
35
index.js
@@ -1,6 +0,6 @@ | ||
var path = require('path') | ||
var murmur32 = require('murmur-32') | ||
var encodeBase32 = require('base32-encode') | ||
import { join } from 'node:path' | ||
import encodeBase32 from 'base32-encode' | ||
import murmur32 from 'murmur-32' | ||
function validateTemplate (template) { | ||
export function validateTemplate (template) { | ||
if (typeof template !== 'string') { | ||
@@ -10,8 +10,8 @@ throw new TypeError('template is not a string') | ||
var re = /(^|[^%])(%%)*%s/ | ||
var first = re.exec(template) | ||
const re = /(^|[^%])(%%)*%s/ | ||
const first = re.exec(template) | ||
if (first === null) throw new Error('No replacement token. Template must contain replacement token %s exactly once') | ||
var pos = first.index + first[0].length | ||
var second = re.exec(template.substring(pos)) | ||
const pos = first.index + first[0].length | ||
const second = re.exec(template.substring(pos)) | ||
if (second !== null) throw new Error('Multiple replacement tokens. Template must contain replacement token %s exactly once') | ||
@@ -21,20 +21,15 @@ } | ||
function replaceToken (template, noise) { | ||
return template.replace(/%([%s])/g, function ($0, $1) { | ||
return ($1 === 's' ? noise : $1) | ||
}) | ||
return template.replace(/%([%s])/g, (_, $1) => ($1 === 's' ? noise : $1)) | ||
} | ||
var invocations = 0 | ||
var localRandom = String(Math.random()) | ||
let invocations = 0 | ||
const localRandom = String(Math.random()) | ||
function randomPath (directory, template) { | ||
export default function randomPath (directory, template) { | ||
validateTemplate(template) | ||
var hash = murmur32(localRandom + String(process.pid) + String(++invocations)) | ||
var noise = encodeBase32(hash, 'Crockford') | ||
const hash = murmur32(localRandom + String(process.pid) + String(++invocations)) | ||
const noise = encodeBase32(hash, 'Crockford') | ||
return path.join(directory, replaceToken(template, noise)) | ||
return join(directory, replaceToken(template, noise)) | ||
} | ||
module.exports = randomPath | ||
module.exports.validateTemplate = validateTemplate |
{ | ||
"name": "random-path", | ||
"version": "0.1.2", | ||
"version": "1.0.0", | ||
"license": "MIT", | ||
"repository": "LinusU/node-random-path", | ||
"type": "module", | ||
"exports": "./index.js", | ||
"scripts": { | ||
"test": "standard && node test" | ||
"test": "standard && node test && ts-readme-generator --check" | ||
}, | ||
"dependencies": { | ||
"base32-encode": "^0.1.0 || ^1.0.0", | ||
"murmur-32": "^0.1.0 || ^0.2.0" | ||
"base32-encode": "^2.0.0", | ||
"murmur-32": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"standard": "^14.3.4" | ||
"standard": "^16.0.3", | ||
"ts-readme-generator": "^0.5.1" | ||
}, | ||
"engines": { | ||
"node": "^12.20.0 || ^14.13.1 || >=16.0.0" | ||
} | ||
} |
@@ -14,4 +14,4 @@ # Random Path | ||
```js | ||
const os = require('os') | ||
const randomPath = require('random-path') | ||
import os from 'node:os' | ||
import randomPath from 'random-path' | ||
@@ -28,20 +28,17 @@ const path = randomPath(os.tmpDir(), '%s.txt') | ||
- `directory` (`string`, required) | ||
- `template` (`string`, required) | ||
- returns `string` - the generated path | ||
Generates a random path name with the specified `directory` and `template`. | ||
`template` should be a string where `%s` will be replaced with some random | ||
characters (e.g. `'linusu-%s'`). The string should contain `%s` exactly once. If | ||
you want to include a literal percent sign, escape it with another one, e.g. | ||
`'%%string'` becomes `'%string'`. | ||
`template` should be a string where `%s` will be replaced with some random characters (e.g. `'linusu-%s'`). The string should contain `%s` exactly once. If you want to include a literal percent sign, escape it with another one, e.g. `'%%string'` becomes `'%string'`. | ||
Returns a string with the path. | ||
**Important:** This module makes no guarantees on wether there exists a file at the returned path or not. Do not simply write data to the returned path. If you want a random file, use the higher level module [fs-temp](https://github.com/LinusU/fs-temp). | ||
**Important:** This module makes no guarantees on wether there exists a | ||
file at the returned path or not. Do not simply write data to the | ||
returned path. If you want a random file, use the higher level module | ||
[fs-temp](https://github.com/LinusU/fs-temp). | ||
### `validateTemplate(template)` | ||
### `randomPath.validateTemplate(template)` | ||
- `template` (`string`, required) | ||
Check to see if the template is a valid template accepted by | ||
`randomPath`. Throws an error if the template is invalid. | ||
Check to see if the template is a valid template accepted by `randomPath`. Throws an error if the template is invalid. | ||
@@ -48,0 +45,0 @@ ## See also |
26
test.js
@@ -1,3 +0,3 @@ | ||
var assert = require('assert') | ||
var randomPath = require('./') | ||
import assert from 'node:assert' | ||
import randomPath, { validateTemplate } from './index.js' | ||
@@ -18,24 +18,24 @@ /* Invalid templates */ | ||
assert.throws(function () { | ||
randomPath.validateTemplate('bad template') | ||
validateTemplate('bad template') | ||
}) | ||
assert.throws(function () { | ||
randomPath.validateTemplate('one: %s, two: %s') | ||
validateTemplate('one: %s, two: %s') | ||
}) | ||
assert.throws(function () { | ||
randomPath.validateTemplate([1, 2]) | ||
validateTemplate([1, 2]) | ||
}) | ||
/* Valid templates */ | ||
randomPath.validateTemplate('%s') | ||
randomPath.validateTemplate('%s.txt') | ||
randomPath.validateTemplate('test-%s') | ||
randomPath.validateTemplate('test-%s.exe') | ||
randomPath.validateTemplate('random => %s') | ||
validateTemplate('%s') | ||
validateTemplate('%s.txt') | ||
validateTemplate('test-%s') | ||
validateTemplate('test-%s.exe') | ||
validateTemplate('random => %s') | ||
/* Valid path */ | ||
var a = randomPath('/tmp', 'test-%s.txt') | ||
var b = randomPath('/tmp', 'test-%s.txt') | ||
var c = randomPath('/tmp', 'test-%s.txt') | ||
const a = randomPath('/tmp', 'test-%s.txt') | ||
const b = randomPath('/tmp', 'test-%s.txt') | ||
const c = randomPath('/tmp', 'test-%s.txt') | ||
@@ -42,0 +42,0 @@ assert.notStrictEqual(a, b) |
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 v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
5440
6
74
1
Yes
2
46
+ Addedbase32-encode@2.0.0(transitive)
+ Addedencode-utf8@2.0.0(transitive)
+ Addedfmix@1.0.0(transitive)
+ Addedmurmur-32@1.0.0(transitive)
+ Addedto-data-view@2.0.0(transitive)
- Removedbase32-encode@1.2.0(transitive)
- Removedencode-utf8@1.0.3(transitive)
- Removedfmix@0.1.0(transitive)
- Removedimul@1.0.1(transitive)
- Removedmurmur-32@0.2.0(transitive)
- Removedto-data-view@1.1.0(transitive)
Updatedbase32-encode@^2.0.0
Updatedmurmur-32@^1.0.0