Skip to content

Commit

Permalink
feat: add new formatter function toDecimal
Browse files Browse the repository at this point in the history
  • Loading branch information
Darlei Kroth committed Feb 11, 2021
1 parent 457a993 commit 0556207
Show file tree
Hide file tree
Showing 6 changed files with 1,087 additions and 1,161 deletions.
12 changes: 2 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
import jsFormatter from './lib/formatter';
import jsGuid from './lib/guid';

export const formatter = jsFormatter;
export const generateGUID = jsGuid;

export default {
formatter,
generateGUID,
};
export { formatter } from './lib/formatter';
export { generateGUID } from './lib/guid';
29 changes: 27 additions & 2 deletions lib/formatter/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import decimal from './decimal';

export default {
export const formatter = {
/**
* Result example: `10` => `10,00`
* @param {Number | String} num
Expand Down Expand Up @@ -67,6 +67,31 @@ export default {
return str.normalize('NFD').replace(/([\u0300-\u036f]|[^0-9a-zA-Z])/g, '');
},

/**
* Result example: `12,30` => `12.3`
* @param {String} value
* @returns {Number} `value` converted to a number type
*/
toDecimal(value = '') {
if (typeof value === 'number') {
return value;
}

if (typeof value !== 'string') {
throw new Error('toDecimal: the value must be to type string');
}

const v = value.split(',');

if (v.length < 2) {
return Number(v);
}

const v0 = this.removeSpecialChars(v[0]);
const v1 = this.removeSpecialChars(v[1]);
return Number(`${v0}.${v1}`);
},

/**
* Result example: `great super title` => `Great Super Title`
* @param {String} title
Expand All @@ -79,4 +104,4 @@ export default {
}
);
},
};
};
66 changes: 59 additions & 7 deletions lib/guid/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,62 @@
import lib from './lib';

/**
* Generate a unique ID based on timestamp
* Fancy ID generator that creates 20-character string identifiers with the following properties:
*
* 1. They're based on timestamp so that they sort *after* any existing ids.
* 2. They contain 72-bits of random data after the timestamp so that IDs won't collide with other clients' IDs.
* 3. They sort *lexicographically* (so the timestamp is converted to characters that will sort properly).
* 4. They're monotonically increasing. Even if you generate more than one in the same timestamp, the
* latter ones will sort after the former ones. We do this by using the previous random bits
* but "incrementing" them by 1 (only in the case of a timestamp collision).
*/
function generateGUID() {
return lib();
}
export const generateGUID = (function () {
// Modeled after base64 web-safe chars, but ordered by ASCII.
var PUSH_CHARS =
"-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";

// Timestamp of last push, used to prevent local collisions if you push twice in one ms.
var lastPushTime = 0;

// We generate 72-bits of randomness which get turned into 12 characters and appended to the
// timestamp to prevent collisions with other clients. We store the last characters we
// generated because in the event of a collision, we'll use those same characters except
// "incremented" by one.
var lastRandChars = [];

return function () {
var now = new Date().getTime();
var duplicateTime = (now === lastPushTime);
lastPushTime = now;

var timeStampChars = new Array(8);
for (var i = 7; i >= 0; i--) {
timeStampChars[i] = PUSH_CHARS.charAt(now % 64);
// NOTE: Can't use << here because javascript will convert to int and lose the upper bits.
now = Math.floor(now / 64);
}
if (now !== 0) {
throw new Error("We should have converted the entire timestamp.");
}

var id = timeStampChars.join("");

if (!duplicateTime) {
for (i = 0; i < 12; i++) {
lastRandChars[i] = Math.floor(Math.random() * 64);
}
} else {
// If the timestamp hasn't changed since last push, use the same random number, except incremented by 1.
for (i = 11; i >= 0 && lastRandChars[i] === 63; i--) {
lastRandChars[i] = 0;
}
lastRandChars[i]++;
}
for (i = 0; i < 12; i++) {
id += PUSH_CHARS.charAt(lastRandChars[i]);
}
if (id.length !== 20) {
throw new Error("Length should be 20.");
}

export default generateGUID;
return id;
};
})();
57 changes: 0 additions & 57 deletions lib/guid/lib.js

This file was deleted.

0 comments on commit 0556207

Please sign in to comment.