Comparing version 0.1.0 to 0.2.0
@@ -5,2 +5,10 @@ # Changelog | ||
## [0.2.0](https://github.com/oftherivier/fictional/compare/v0.1.0...v0.2.0) (2020-05-17) | ||
### Features | ||
* **char:** Change char range shorthands ([037b216](https://github.com/oftherivier/fictional/commit/037b2167fe69292ab273a96f1b5e212562080375)) | ||
* Faster char() for ranges < 256 ([ea745ea](https://github.com/oftherivier/fictional/commit/ea745ea44fb342b60fd31c5dde967c342d54c74b)) | ||
## 0.1.0 (2020-05-16) | ||
@@ -7,0 +15,0 @@ |
58
char.js
@@ -5,41 +5,43 @@ var hash = require('./hash') | ||
var char = charInRanges([ | ||
// ascii | ||
[0x61, 0x7a], | ||
var VALUES_THRESHOLD = 256 | ||
// latin1 | ||
[0x0020, 0x007e], | ||
[0x00a0, 0x00ff] | ||
var ascii = charInRanges([[0x20, 0x7e]]) | ||
var latin1 = charInRanges([ | ||
[0x20, 0x7e], | ||
[0xa0, 0xff] | ||
]) | ||
char.inRanges = charInRanges | ||
char.unicode = char | ||
char.ascii = charInRanges([[0x61, 0x7a]]) | ||
char.latin1 = charInRanges([ | ||
[0x0020, 0x007e], | ||
[0x00a0, 0x00ff] | ||
var digit = charInRanges([[0x30, 0x39]]) | ||
var asciiLower = charInRanges([[0x61, 0x7a]]) | ||
var asciiUpper = charInRanges([[0x41, 0x5a]]) | ||
var latin1Lower = charInRanges([ | ||
[0xc0, 0xd6], | ||
[0xd8, 0xde] | ||
]) | ||
char.asciiLower = charInRanges([[0x61, 0x7a]]) | ||
char.asciiUpper = charInRanges([[0x41, 0x5a]]) | ||
char.digit = charInRanges([[0x30, 0x39]]) | ||
char.latin1Upper = charInRanges([ | ||
var latin1Upper = charInRanges([ | ||
[0xdf, 0xf6], | ||
[0xf8, 0xff] | ||
]) | ||
char.latin1Lower = charInRanges([ | ||
[0xc0, 0xd6], | ||
[0xd8, 0xde] | ||
]) | ||
char.asciiLetter = charInRanges([char.asciiLower, char.asciiUpper]) | ||
char.latin1Letter = charInRanges([char.latin1Lower, char.latin1Upper]) | ||
char.alphanumeric = charInRanges([char.asciiLetter, char.digit]) | ||
char.lower = charInRanges([char.asciiLower, char.latin1Lower]) | ||
char.upper = charInRanges([char.latin1Upper, char.latin1Upper]) | ||
char.letter = charInRanges([char.asciiLetter, char.latin1Letter]) | ||
var char = charInRanges([asciiLower, asciiUpper, digit]) | ||
char.inRanges = charInRanges | ||
char.alphanumeric = charInRanges([char]) | ||
char.ascii = ascii | ||
char.latin1 = latin1 | ||
char.unicode = charInRanges([ascii, latin1]) | ||
char.digit = digit | ||
char.lower = char.asciiLower = asciiLower | ||
char.upper = char.asciiUpper = asciiUpper | ||
char.latin1Lower = latin1Lower | ||
char.latin1Upper = latin1Upper | ||
char.letter = char.asciiLetter = charInRanges([asciiLower, asciiUpper]) | ||
char.latin1Letter = charInRanges([latin1Lower, latin1Upper]) | ||
char.alphanumeric = charInRanges([asciiLower, asciiUpper, digit]) | ||
char.unicodeLower = charInRanges([asciiLower, latin1Lower]) | ||
char.unicodeUpper = charInRanges([asciiUpper, latin1Upper]) | ||
function charInRanges(ranges) { | ||
ranges = normalizeRanges(ranges) | ||
var fitFn = fitRanges(ranges) | ||
var fitFn = fitRanges(ranges, VALUES_THRESHOLD) | ||
@@ -46,0 +48,0 @@ charFn.__fictional_char = { |
{ | ||
"name": "fictional", | ||
"description": "Generate fake data deterministically from a given input", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"type": "commonjs", | ||
@@ -6,0 +6,0 @@ "main": "index.js", |
@@ -47,2 +47,3 @@ # `fictional` | ||
- [Why](#why) | ||
- [Overview](#overview) | ||
@@ -392,6 +393,6 @@ - [Makers](#overview-makers) | ||
// => | ||
'u' | ||
'h' | ||
``` | ||
The generated character will be any [ASCII](https://en.wikipedia.org/wiki/ASCII) or [Latin-1 Supplement](https://en.wikipedia.org/wiki/Latin-1_Supplement_(Unicode_block)) character. Alternative character ranges are listed [below](#char-ranges). To choose your own range of characters, see [`char.inRanges()`](#char-in-ranges). | ||
The generated character will be an alphanumeric: lower and upper case ASCII letters and digits 0 to 9. Alternative character ranges are listed [below](#char-ranges). To choose your own range of characters, see [`char.inRanges()`](#char-in-ranges). | ||
@@ -403,3 +404,3 @@ ##### Predefined character ranges | ||
// => | ||
'g' | ||
'=' | ||
@@ -413,17 +414,51 @@ char.digit('id-3') | ||
* `char.ascii`: Any [ASCII](https://en.wikipedia.org/wiki/ASCII) character | ||
* `char.ascii`: Any ASCII character | ||
* `char.digit`: Characters for numbers 0 to 9 | ||
* `char.alphanumeric`: Characters for both numbers 0 to 9 and lower and upper case ASCII letters | ||
* `char.letter`: Lower and upper case letters from [ASCII](https://en.wikipedia.org/wiki/ASCII) or [Latin-1 Supplement](https://en.wikipedia.org/wiki/Latin-1_Supplement_(Unicode_block)) | ||
* `char.lower`: Lower case letters from ASCII or Latin-1 Supplement | ||
* `char.upper`: Upper case letters from ASCII or Latin-1 Supplement | ||
* `char.asciiLower`: Lower case [ASCII](https://en.wikipedia.org/wiki/ASCII) letters | ||
* `char.asciiUpper`: Upper case ASCII letters | ||
* `char.latin1Lower`: Lower case [Latin-1 Supplement](https://en.wikipedia.org/wiki/Latin-1_Supplement_(Unicode_block)) letters | ||
* `char.alphanumeric` (alias: `char`): lower and upper case ASCII letters and digits 0 to 9 | ||
* `char.letter` (alias: `char.asciiLetter`): Lower and upper case ASCII letters | ||
* `char.lower` (alias: `asciiLower`): Lower case ASCII letters | ||
* `char.upper` (alias `char.asciiUpper`): Upper case ASCII letters | ||
* `char.unicode`: Any character from the ASCII and [Latin-1 Supplement](https://en.wikipedia.org/wiki/Latin-1_Supplement_(Unicode_block)) unicode blocks | ||
* `char.unicodeLetter`: Lower and upper case letters from the ASCII and Latin-1 Supplement unicode blocks | ||
* `char.unicodeLower`: Lower case letters from the ASCII and Latin-1 Supplement unicode blocks | ||
* `char.unicodeUpper`: Upper case letters from the ASCII and Latin-1 Supplement unicode blocks | ||
* `char.latin1`: Any character from the Latin-1 Supplement unicode block | ||
* `char.latin1Letter`: Lower and upper case Latin-1 Supplement letters | ||
* `char.latin1Lower`: Lower case Latin-1 Supplement letters | ||
* `char.latin1Upper`: Upper case Latin-1 Supplement letters | ||
* `char.asciiLetter`: Lower and upper case ASCII letters | ||
* `char.latin1Letter`: Lower and upper case Latin-1 Supplement letters | ||
##### <a name="char-in-ranges" href="#char-in-ranges">#</a> `char.inRanges(ranges)` | ||
Takes in an array of `[min, max]` pairs, where `min` and `max` are integers specifying the minimum and maximum possible [Unicode code point](https://en.wikipedia.org/wiki/List_of_Unicode_characters) values for a desired range of characters, and returns a maker function that will return characters in those given ranges. | ||
```js | ||
const symbols = char.inRanges([ | ||
// misc symbols | ||
[0x2600, 0x26ff], | ||
// emoticons | ||
[0x1f600, 0x1f64f] | ||
]) | ||
symbols('id-23') | ||
// => | ||
'⛑' | ||
``` | ||
`char.inRanges` is designed to allow characters in the ranges given to all have a similar likelihood of being returned. | ||
To allow for composition, each item in the array of `ranges` can also be a pre-defined character range, or another character range defined using `char.inRanges()`: | ||
```js | ||
const misc = char.inRanges([[0x2600, 0x26ff]]) | ||
const emoticons = char.inRanges([[0x1f600, 0x1f64f]]) | ||
const letterOrSymbol = char.inRanges([misc, emoticons, char.letter]) | ||
letterOrSymbol('id-3') | ||
// => | ||
'⛵' | ||
``` | ||
#### <a name="word" href="#word">#</a> `word(id[, options])` | ||
@@ -603,3 +638,3 @@ | ||
// => | ||
'M1jL' | ||
'o1jL' | ||
``` | ||
@@ -657,3 +692,3 @@ | ||
[ | ||
'Í', | ||
'F', | ||
'Yoranö' | ||
@@ -697,4 +732,4 @@ ] | ||
[ | ||
'ï', | ||
'w' | ||
'2', | ||
'V' | ||
] | ||
@@ -709,3 +744,3 @@ ``` | ||
[ | ||
'í', | ||
'w', | ||
'!' | ||
@@ -712,0 +747,0 @@ ] |
@@ -12,3 +12,3 @@ /* IMPORTANT | ||
"bool": true, | ||
"char": "ì", | ||
"char": "m", | ||
"dateString": "2017-06-10T05:23:20.000Z", | ||
@@ -46,3 +46,3 @@ "float": 1286777711.218018, | ||
"bool": false, | ||
"char": "î", | ||
"char": "M", | ||
"dateString": "2016-05-09T04:22:19.000Z", | ||
@@ -80,3 +80,3 @@ "float": 1286777614.1717145, | ||
"bool": true, | ||
"char": "«", | ||
"char": "2", | ||
"dateString": "2019-08-12T07:25:22.000Z", | ||
@@ -117,3 +117,3 @@ "float": 1286777645.2050598, | ||
"bool": false, | ||
"char": "e", | ||
"char": "0", | ||
"dateString": "2018-07-11T06:24:21.000Z", | ||
@@ -153,3 +153,3 @@ "float": 1286777804.5901115, | ||
"bool": true, | ||
"char": "Ë", | ||
"char": "z", | ||
"dateString": "2013-02-06T01:19:16.000Z", | ||
@@ -187,3 +187,3 @@ "float": 1286777579.397858, | ||
"bool": false, | ||
"char": "ö", | ||
"char": "w", | ||
"dateString": "2012-01-05T00:18:15.000Z", | ||
@@ -221,3 +221,3 @@ "float": 1286777482.2707348, | ||
"bool": true, | ||
"char": "®", | ||
"char": "G", | ||
"dateString": "2015-04-08T03:21:18.000Z", | ||
@@ -258,3 +258,3 @@ "float": 1286777513.2978244, | ||
"bool": false, | ||
"char": "k", | ||
"char": "8", | ||
"dateString": "2014-03-07T02:20:17.000Z", | ||
@@ -294,3 +294,3 @@ "float": 1286777672.328298, | ||
"bool": true, | ||
"char": "ë", | ||
"char": "4", | ||
"dateString": "1985-02-18T13:31:28.000Z", | ||
@@ -329,3 +329,3 @@ "float": 1286777959.1906652, | ||
"bool": false, | ||
"char": "i", | ||
"char": "P", | ||
"dateString": "1984-01-17T12:30:27.000Z", | ||
@@ -364,3 +364,3 @@ "float": 1286777862.2155948, | ||
"bool": false, | ||
"char": "£", | ||
"char": "m", | ||
"dateString": "1992-09-25T20:39:59.000Z", | ||
@@ -399,3 +399,3 @@ "float": 3808955390.2405596, | ||
"bool": true, | ||
"char": "g", | ||
"char": "q", | ||
"dateString": "1985-06-02T05:13:32.000Z", | ||
@@ -435,3 +435,3 @@ "float": 3808958399.329893, | ||
"bool": false, | ||
"char": "\\\\", | ||
"char": "y", | ||
"dateString": "2018-03-07T14:47:05.000Z", | ||
@@ -471,3 +471,3 @@ "float": 3808957564.213242, | ||
"bool": true, | ||
"char": "Î", | ||
"char": "N", | ||
"dateString": "2011-12-12T23:20:38.000Z", | ||
@@ -508,3 +508,3 @@ "float": 3808960573.854489, | ||
"bool": false, | ||
"char": "ö", | ||
"char": "J", | ||
"dateString": "1980-09-05T08:25:47.000Z", | ||
@@ -543,3 +543,3 @@ "float": 3808951034.241488, | ||
"bool": true, | ||
"char": "¼", | ||
"char": "g", | ||
"dateString": "2013-06-10T17:59:20.000Z", | ||
@@ -579,3 +579,3 @@ "float": 3808954043.213394, | ||
"bool": false, | ||
"char": "]", | ||
"char": "T", | ||
"dateString": "2006-03-15T02:32:53.000Z", | ||
@@ -615,3 +615,3 @@ "float": 3808953208.171148, | ||
"bool": true, | ||
"char": "Ù", | ||
"char": "s", | ||
"dateString": "1999-12-20T12:06:26.000Z", | ||
@@ -652,3 +652,3 @@ "float": 3808956217.402405, | ||
"bool": false, | ||
"char": "Å", | ||
"char": "g", | ||
"dateString": "2016-09-09T21:08:23.000Z", | ||
@@ -685,3 +685,3 @@ "float": 3808964086.2200127, | ||
"bool": true, | ||
"char": "U", | ||
"char": "L", | ||
"dateString": "2009-06-14T05:41:56.000Z", | ||
@@ -719,3 +719,3 @@ "float": 3808967095.2873507, | ||
"bool": true, | ||
"char": "Ú", | ||
"char": "N", | ||
"dateString": "1995-12-28T23:43:02.000Z", | ||
@@ -757,3 +757,3 @@ "float": 3808955293.4265146, | ||
"bool": false, | ||
"char": "æ", | ||
"char": "r", | ||
"dateString": "1986-07-03T06:14:33.000Z", | ||
@@ -793,3 +793,3 @@ "float": 3808958492.2532563, | ||
"bool": true, | ||
"char": "È", | ||
"char": "c", | ||
"dateString": "2017-02-06T13:46:04.000Z", | ||
@@ -827,3 +827,3 @@ "float": 3808957343.9904304, | ||
"bool": false, | ||
"char": "Í", | ||
"char": "f", | ||
"dateString": "2008-09-09T20:17:35.000Z", | ||
@@ -862,3 +862,3 @@ "float": 3808960542.40711, | ||
"bool": true, | ||
"char": "ò", | ||
"char": "L", | ||
"dateString": "1983-12-08T11:28:50.000Z", | ||
@@ -900,3 +900,3 @@ "float": 3808950937.234075, | ||
"bool": false, | ||
"char": "Ú", | ||
"char": "i", | ||
"dateString": "2014-07-11T19:00:21.000Z", | ||
@@ -936,3 +936,3 @@ "float": 3808954136.1439285, | ||
"bool": true, | ||
"char": ",", | ||
"char": "C", | ||
"dateString": "2005-02-14T01:31:52.000Z", | ||
@@ -970,3 +970,3 @@ "float": 3808952987.1967998, | ||
"bool": false, | ||
"char": "W", | ||
"char": "6", | ||
"dateString": "1996-09-17T09:03:23.000Z", | ||
@@ -1005,3 +1005,3 @@ "float": 3808956186.11154, | ||
"bool": true, | ||
"char": "P", | ||
"char": "F", | ||
"dateString": "2019-12-13T00:11:26.000Z", | ||
@@ -1041,3 +1041,3 @@ "float": 3808963989.207228, | ||
"bool": false, | ||
"char": "o", | ||
"char": "9", | ||
"dateString": "2010-07-15T06:42:57.000Z", | ||
@@ -1078,3 +1078,3 @@ "float": 3808967188.3802156, | ||
"bool": false, | ||
"char": "Ã", | ||
"char": "N", | ||
"dateString": "1994-11-27T22:42:01.000Z", | ||
@@ -1113,3 +1113,3 @@ "float": 3808955324.530615, | ||
"bool": true, | ||
"char": "Ò", | ||
"char": "I", | ||
"dateString": "1987-08-04T07:15:34.000Z", | ||
@@ -1151,3 +1151,3 @@ "float": 3808958589.128241, | ||
"bool": false, | ||
"char": "D", | ||
"char": "g", | ||
"dateString": "2016-01-05T12:45:03.000Z", | ||
@@ -1185,3 +1185,3 @@ "float": 3808957374.215202, | ||
"bool": true, | ||
"char": "¡", | ||
"char": "V", | ||
"dateString": "2009-10-10T21:18:36.000Z", | ||
@@ -1219,3 +1219,3 @@ "float": 3808960639.334581, | ||
"bool": false, | ||
"char": "3", | ||
"char": "p", | ||
"dateString": "1982-11-07T10:27:49.000Z", | ||
@@ -1254,3 +1254,3 @@ "float": 3808950968.412351, | ||
"bool": true, | ||
"char": ".", | ||
"char": "c", | ||
"dateString": "2015-08-12T20:01:22.000Z", | ||
@@ -1292,3 +1292,3 @@ "float": 3808954233.1833787, | ||
"bool": false, | ||
"char": ":", | ||
"char": "u", | ||
"dateString": "2004-01-13T00:30:51.000Z", | ||
@@ -1326,3 +1326,3 @@ "float": 3808953018.157132, | ||
"bool": true, | ||
"char": ")", | ||
"char": "P", | ||
"dateString": "1997-10-18T10:04:24.000Z", | ||
@@ -1360,3 +1360,3 @@ "float": 3808956283.1614156, | ||
"bool": false, | ||
"char": "Û", | ||
"char": "b", | ||
"dateString": "2018-11-11T23:10:25.000Z", | ||
@@ -1396,3 +1396,3 @@ "float": 3808964020.208443, | ||
"bool": true, | ||
"char": "J", | ||
"char": "m", | ||
"dateString": "2011-08-16T07:43:58.000Z", | ||
@@ -1432,3 +1432,3 @@ "float": 3808967285.542157, | ||
"bool": true, | ||
"char": "'", | ||
"char": "s", | ||
"dateString": "1989-06-22T17:36:56.000Z", | ||
@@ -1468,3 +1468,3 @@ "float": 3808955227.2368965, | ||
"bool": false, | ||
"char": "e", | ||
"char": "5", | ||
"dateString": "1980-01-25T00:08:27.000Z", | ||
@@ -1502,3 +1502,3 @@ "float": 3808958426.37953, | ||
"bool": true, | ||
"char": "¯", | ||
"char": "o", | ||
"dateString": "2015-12-04T11:44:02.000Z", | ||
@@ -1539,3 +1539,3 @@ "float": 3808957401.261505, | ||
"bool": false, | ||
"char": "Ñ", | ||
"char": "l", | ||
"dateString": "2006-07-07T18:15:33.000Z", | ||
@@ -1574,3 +1574,3 @@ "float": 3808960600.4004135, | ||
"bool": true, | ||
"char": "¡", | ||
"char": "H", | ||
"dateString": "1985-02-10T13:30:52.000Z", | ||
@@ -1610,3 +1610,3 @@ "float": 3808951135.2813907, | ||
"bool": false, | ||
"char": "7", | ||
"char": "A", | ||
"dateString": "2016-09-13T21:02:23.000Z", | ||
@@ -1644,3 +1644,3 @@ "float": 3808954334.119346, | ||
"bool": true, | ||
"char": "û", | ||
"char": "O", | ||
"dateString": "2011-08-20T07:37:58.000Z", | ||
@@ -1681,3 +1681,3 @@ "float": 3808953309.1834393, | ||
"bool": false, | ||
"char": "å", | ||
"char": "A", | ||
"dateString": "2002-03-23T14:09:29.000Z", | ||
@@ -1716,3 +1716,3 @@ "float": 3808956508.123964, | ||
"bool": true, | ||
"char": "?", | ||
"char": "o", | ||
"dateString": "2013-06-06T18:05:20.000Z", | ||
@@ -1750,3 +1750,3 @@ "float": 3808963923.506448, | ||
"bool": false, | ||
"char": "¹", | ||
"char": "w", | ||
"dateString": "2004-01-09T00:36:51.000Z", | ||
@@ -1753,0 +1753,0 @@ "float": 3808967122.223632, |
var hash = require('../hash') | ||
var expandRange = require('./expandRange') | ||
module.exports = function fitRanges(ranges) { | ||
module.exports = function fitRanges(ranges, valuesThreshold) { | ||
if (!ranges.length) { | ||
@@ -8,14 +9,24 @@ throw new Error('Empty range set given to fitRanges()') | ||
var meta = computeRangeMetadata(ranges.slice().sort(compareRanges)) | ||
var offsets = meta.offsets | ||
var meta = computeRangeMetadata( | ||
ranges.slice().sort(compareRanges), | ||
valuesThreshold | ||
) | ||
var values = meta.values | ||
var segments = meta.segments | ||
var size = meta.size | ||
fitRangesFn.meta = meta | ||
return fitRangesFn | ||
var fn = values | ||
? fitRangesByValues(values) | ||
: fitRangesByOffsets(segments, size) | ||
function fitRangesFn(input) { | ||
fn.meta = meta | ||
return fn | ||
} | ||
function fitRangesByOffsets(segments, size) { | ||
return function fitRangesByOffsetsFn(input) { | ||
var key = hash([input, 'fitRanges']) % size | ||
var l = 0 | ||
var r = offsets.length - 1 | ||
var r = segments.length - 1 | ||
var m | ||
@@ -28,5 +39,5 @@ var d | ||
m = Math.floor((l + r) / 2) | ||
d = offsets[m] | ||
min = d.min | ||
max = d.max | ||
d = segments[m] | ||
min = d.modMin | ||
max = d.modMax | ||
@@ -42,2 +53,10 @@ if (key > max) l = m + 1 | ||
function fitRangesByValues(values) { | ||
var n = values.length | ||
return function fitRangesByValuesFn(input) { | ||
return values[hash([input, 'fitRanges']) % n] | ||
} | ||
} | ||
function compareRanges(a, b) { | ||
@@ -55,4 +74,4 @@ var aMin = a[0] | ||
function computeRangeMetadata(ranges) { | ||
var offsets = [] | ||
function computeRangeMetadata(ranges, valuesThreshold) { | ||
var segments = [] | ||
var n = ranges.length | ||
@@ -78,5 +97,7 @@ var i = 0 | ||
offsets.push({ | ||
min: modMin, | ||
max: modMax, | ||
segments.push({ | ||
modMin: modMin, | ||
modMax: modMax, | ||
min: currentMin, | ||
max: currentMax, | ||
offset: currentMin - modMin | ||
@@ -93,6 +114,7 @@ }) | ||
offsets.push({ | ||
ranges: ranges, | ||
min: modMin, | ||
max: modMin + (currentMax - currentMin), | ||
segments.push({ | ||
modMin: modMin, | ||
modMax: modMin + (currentMax - currentMin), | ||
min: currentMin, | ||
max: currentMax, | ||
offset: currentMin - modMin | ||
@@ -105,5 +127,20 @@ }) | ||
ranges: ranges, | ||
offsets: offsets, | ||
size: size | ||
segments: segments, | ||
size: size, | ||
values: size <= valuesThreshold ? computeRangeValues(segments) : null | ||
} | ||
} | ||
function computeRangeValues(segments) { | ||
var i = -1 | ||
var n = segments.length | ||
var d | ||
var results = [] | ||
while (++i < n) { | ||
d = segments[i] | ||
results.push.apply(results, expandRange(d.min, d.max)) | ||
} | ||
return results | ||
} |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
5242122
66
5355
846
0