Skip to content

Commit

Permalink
Create vue-uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
VitorLuizC committed Oct 27, 2017
0 parents commit f8a1202
Show file tree
Hide file tree
Showing 9 changed files with 308 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
root = true

# Default editor's settings
[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Node.js dependencies
node_modules/

# Lock files
# Because libs don't need it
package-lock.json
yarn.lock
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Vue UUID

Add UUID to Vue instance.

## Installation

Installation is very easy, you just need to install using NPM or Yarn.

```sh
npm i vue-uuid
```

Vue's `use` method will do the trick adding to Vue.

```js
import Vue from 'vue'
import UUID from 'vue-uuid'

Vue.use(UUID)
```

## Usage

After installation `$uuid` is available on instance, so you can use inside
components **template** and script, like the example below.

```vue
<template>
<div class="uuid-panel">
<h3 class="uuid">{{ uuid }}</h3>
<button
class="button"
@click="uuid = $uuid.v1()"
>Generate V1</button>
<button
class="button"
@click="uuid = $uuid.v4()"
>Generate V4</button>
</div>
</template>
<script>
export default {
data () {
return {
uuid: this.$uuid() // is equals to this.$uuid.v4()
}
}
}
</script>
```
11 changes: 11 additions & 0 deletions dist/vue-uuid.common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var uuid = _interopDefault(require('uuid'));

var install = function (Vue) {
Vue.prototype.$uuid = uuid;
};

module.exports = install;
182 changes: 182 additions & 0 deletions dist/vue-uuid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('crypto')) :
typeof define === 'function' && define.amd ? define(['crypto'], factory) :
(global.vueUuid = factory(global.crypto));
}(this, (function (crypto) { 'use strict';

crypto = crypto && crypto.hasOwnProperty('default') ? crypto['default'] : crypto;

// Unique ID creation requires a high quality random # generator. In node.js
// this is pretty straight-forward - we use the crypto API.

var rb = crypto.randomBytes;

function rng() {
return rb(16);
}

var rng_1 = rng;

/**
* Convert array of 16 byte values to UUID string format of the form:
* XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
*/
var byteToHex = [];
for (var i = 0; i < 256; ++i) {
byteToHex[i] = (i + 0x100).toString(16).substr(1);
}

function bytesToUuid(buf, offset) {
var i = offset || 0;
var bth = byteToHex;
return bth[buf[i++]] + bth[buf[i++]] +
bth[buf[i++]] + bth[buf[i++]] + '-' +
bth[buf[i++]] + bth[buf[i++]] + '-' +
bth[buf[i++]] + bth[buf[i++]] + '-' +
bth[buf[i++]] + bth[buf[i++]] + '-' +
bth[buf[i++]] + bth[buf[i++]] +
bth[buf[i++]] + bth[buf[i++]] +
bth[buf[i++]] + bth[buf[i++]];
}

var bytesToUuid_1 = bytesToUuid;

// **`v1()` - Generate time-based UUID**
//
// Inspired by https://github.com/LiosK/UUID.js
// and http://docs.python.org/library/uuid.html

// random #'s we need to init node and clockseq
var _seedBytes = rng_1();

// Per 4.5, create and 48-bit node id, (47 random bits + multicast bit = 1)
var _nodeId = [
_seedBytes[0] | 0x01,
_seedBytes[1], _seedBytes[2], _seedBytes[3], _seedBytes[4], _seedBytes[5]
];

// Per 4.2.2, randomize (14 bit) clockseq
var _clockseq = (_seedBytes[6] << 8 | _seedBytes[7]) & 0x3fff;

// Previous uuid creation time
var _lastMSecs = 0;
var _lastNSecs = 0;

// See https://github.com/broofa/node-uuid for API details
function v1(options, buf, offset) {
var i = buf && offset || 0;
var b = buf || [];

options = options || {};

var clockseq = options.clockseq !== undefined ? options.clockseq : _clockseq;

// UUID timestamps are 100 nano-second units since the Gregorian epoch,
// (1582-10-15 00:00). JSNumbers aren't precise enough for this, so
// time is handled internally as 'msecs' (integer milliseconds) and 'nsecs'
// (100-nanoseconds offset from msecs) since unix epoch, 1970-01-01 00:00.
var msecs = options.msecs !== undefined ? options.msecs : new Date().getTime();

// Per 4.2.1.2, use count of uuid's generated during the current clock
// cycle to simulate higher resolution clock
var nsecs = options.nsecs !== undefined ? options.nsecs : _lastNSecs + 1;

// Time since last uuid creation (in msecs)
var dt = (msecs - _lastMSecs) + (nsecs - _lastNSecs)/10000;

// Per 4.2.1.2, Bump clockseq on clock regression
if (dt < 0 && options.clockseq === undefined) {
clockseq = clockseq + 1 & 0x3fff;
}

// Reset nsecs if clock regresses (new clockseq) or we've moved onto a new
// time interval
if ((dt < 0 || msecs > _lastMSecs) && options.nsecs === undefined) {
nsecs = 0;
}

// Per 4.2.1.2 Throw error if too many uuids are requested
if (nsecs >= 10000) {
throw new Error('uuid.v1(): Can\'t create more than 10M uuids/sec');
}

_lastMSecs = msecs;
_lastNSecs = nsecs;
_clockseq = clockseq;

// Per 4.1.4 - Convert from unix epoch to Gregorian epoch
msecs += 12219292800000;

// `time_low`
var tl = ((msecs & 0xfffffff) * 10000 + nsecs) % 0x100000000;
b[i++] = tl >>> 24 & 0xff;
b[i++] = tl >>> 16 & 0xff;
b[i++] = tl >>> 8 & 0xff;
b[i++] = tl & 0xff;

// `time_mid`
var tmh = (msecs / 0x100000000 * 10000) & 0xfffffff;
b[i++] = tmh >>> 8 & 0xff;
b[i++] = tmh & 0xff;

// `time_high_and_version`
b[i++] = tmh >>> 24 & 0xf | 0x10; // include version
b[i++] = tmh >>> 16 & 0xff;

// `clock_seq_hi_and_reserved` (Per 4.2.2 - include variant)
b[i++] = clockseq >>> 8 | 0x80;

// `clock_seq_low`
b[i++] = clockseq & 0xff;

// `node`
var node = options.node || _nodeId;
for (var n = 0; n < 6; ++n) {
b[i + n] = node[n];
}

return buf ? buf : bytesToUuid_1(b);
}

var v1_1 = v1;

function v4(options, buf, offset) {
var i = buf && offset || 0;

if (typeof(options) == 'string') {
buf = options == 'binary' ? new Array(16) : null;
options = null;
}
options = options || {};

var rnds = options.random || (options.rng || rng_1)();

// Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
rnds[6] = (rnds[6] & 0x0f) | 0x40;
rnds[8] = (rnds[8] & 0x3f) | 0x80;

// Copy bytes to buffer, if provided
if (buf) {
for (var ii = 0; ii < 16; ++ii) {
buf[i + ii] = rnds[ii];
}
}

return buf || bytesToUuid_1(rnds);
}

var v4_1 = v4;

var uuid = v4_1;
uuid.v1 = v1_1;
uuid.v4 = v4_1;

var uuid_1 = uuid;

var install = function (Vue) {
Vue.prototype.$uuid = uuid_1;
};

return install;

})));
2 changes: 2 additions & 0 deletions dist/vue-uuid.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/vue-uuid.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import uuid from 'uuid'

const install = (Vue) => {
Vue.prototype.$uuid = uuid
}

export default install
37 changes: 37 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "vue-uuid",
"version": "0.1.0",
"description": "Add UUID to Vue instance.",
"main": "dist/index.common.js",
"module": "index.js",
"scripts": {
"test": "standard",
"build": "bili ./index.js -o dist --format cjs --format umd --compress umd",
"prepare": "npm run build"
},
"repository": {
"type": "git",
"url": "git+https://github.com/VitorLuizC/vue-uuid.git"
},
"keywords": [
"vue",
"vue-uuid",
"uuid"
],
"author": "Vitor Luiz Cavalcanti",
"license": "MIT",
"bugs": {
"url": "https://github.com/VitorLuizC/vue-uuid/issues"
},
"homepage": "https://github.com/VitorLuizC/vue-uuid#README",
"devDependencies": {
"bili": "^0.18.2",
"standard": "^10.0.3"
},
"dependencies": {
"uuid": "^3.1.0"
},
"standard": {
"ignore": ["dist/**"]
}
}

0 comments on commit f8a1202

Please sign in to comment.