Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Bug 724428 - provide uuid parsing from uuid module #337

Merged
merged 5 commits into from about 2 years ago

2 participants

Irakli Gozalishvili ochameau
Irakli Gozalishvili
Collaborator

It looks like there are few cases where one might want to parse UUID instead of generating a new one:

https://github.com/mozilla/openwebapps/blob/develop/addons/activities/lib/main.js#L43

This change:

  • Add's a way to parse existing uuid from the uuid module
  • Replaces old license block
  • Adds documentation for uuid module

https://bugzilla.mozilla.org/show_bug.cgi?id=724428

ochameau
Collaborator

I looked at C++ code for Components.ID and generateUUID. Both of them returns nsIJSID
https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsIJSID

Looks good. r+

Irakli Gozalishvili Gozala merged commit 215e619 into from February 06, 2012
Irakli Gozalishvili Gozala closed this February 06, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
27  packages/api-utils/docs/uuid.md
Source Rendered
... ...
@@ -0,0 +1,27 @@
  1
+<!-- This Source Code Form is subject to the terms of the Mozilla Public
  2
+   - License, v. 2.0. If a copy of the MPL was not distributed with this
  3
+   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
  4
+
  5
+Module `uuid` provides low level API for generating / parsing UUID, that may
  6
+be necessary when hacking on internals of the platform.
  7
+
  8
+
  9
+## Generate UUID
  10
+
  11
+Module exports `uuid` function. When called without arguments it will uses
  12
+platform-specific methods to obtain a `nsID` that can be considered to be
  13
+globally unique.
  14
+
  15
+    let uuid = require('api-utils/uuid').uuid()
  16
+
  17
+## Parsing UUID
  18
+
  19
+Sometimes one might need to create `nsID` from an existing UUID string. Same
  20
+`uuid` function may be used to parse such UUID strings into an `nsID`:
  21
+
  22
+    let { uuid } = require('api-utils/uuid');
  23
+    let firefoxUUID = uuid('{ec8030f7-c20a-464f-9b0e-13a3a9e97384}');
  24
+
  25
+For more details about UUID representations and what they are used for by the
  26
+platform see MDN documentation for
  27
+[JSID](https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsIJSID)
50  packages/api-utils/lib/uuid.js
... ...
@@ -1,45 +1,13 @@
1  
-/* vim:set ts=2 sw=2 sts=2 et: */
2  
-/* ***** BEGIN LICENSE BLOCK *****
3  
- * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  
- *
5  
- * The contents of this file are subject to the Mozilla Public License Version
6  
- * 1.1 (the "License"); you may not use this file except in compliance with
7  
- * the License. You may obtain a copy of the License at
8  
- * http://www.mozilla.org/MPL/
9  
- *
10  
- * Software distributed under the License is distributed on an "AS IS" basis,
11  
- * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  
- * for the specific language governing rights and limitations under the
13  
- * License.
14  
- *
15  
- * The Original Code is Jetpack.
16  
- *
17  
- * The Initial Developer of the Original Code is
18  
- * the Mozilla Foundation.
19  
- * Portions created by the Initial Developer are Copyright (C) 2011
20  
- * the Initial Developer. All Rights Reserved.
21  
- *
22  
- * Contributor(s):
23  
- *   Irakli Gozalishvili <gozala@mozilla.com> (Original Author)
24  
- *
25  
- * Alternatively, the contents of this file may be used under the terms of
26  
- * either the GNU General Public License Version 2 or later (the "GPL"), or
27  
- * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28  
- * in which case the provisions of the GPL or the LGPL are applicable instead
29  
- * of those above. If you wish to allow use of your version of this file only
30  
- * under the terms of either the GPL or the LGPL, and not to allow others to
31  
- * use your version of this file under the terms of the MPL, indicate your
32  
- * decision by deleting the provisions above and replace them with the notice
33  
- * and other provisions required by the GPL or the LGPL. If you do not delete
34  
- * the provisions above, a recipient may use your version of this file under
35  
- * the terms of any one of the MPL, the GPL or the LGPL.
36  
- *
37  
- * ***** END LICENSE BLOCK ***** */
  1
+/* This Source Code Form is subject to the terms of the Mozilla Public
  2
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
  3
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
38 4
 
39 5
 "use strict";
40 6
 
41  
-const { Cc, Ci } = require('chrome');
  7
+const { Cc, Ci, components: { ID: parseUUID } } = require('chrome');
  8
+const { generateUUID } = Cc['@mozilla.org/uuid-generator;1'].
  9
+                          getService(Ci.nsIUUIDGenerator);
42 10
 
43  
-exports.uuid = Cc['@mozilla.org/uuid-generator;1'].
44  
-               getService(Ci.nsIUUIDGenerator).
45  
-               generateUUID;
  11
+// Returns `uuid`. If `id` is passed then it's parsed to `uuid` and returned
  12
+// if not then new one is generated.
  13
+exports.uuid = function uuid(id) id ? parseUUID(id) : generateUUID()
10  packages/api-utils/tests/test-uuid.js
@@ -2,7 +2,7 @@
2 2
 
3 3
 const { uuid } = require('api-utils/uuid');
4 4
 
5  
-exports['test uuid'] = function(assert) {
  5
+exports['test generate uuid'] = function(assert) {
6 6
   let signature = /{[0-9a-f\-]+}/
7 7
   let first = String(uuid());
8 8
   let second = String(uuid());
@@ -12,4 +12,12 @@ exports['test uuid'] = function(assert) {
12 12
   assert.notEqual(first, second, 'guid generates new guid on each call');
13 13
 };
14 14
 
  15
+exports['test parse uuid'] = function(assert) {
  16
+  let firefoxUUID = '{ec8030f7-c20a-464f-9b0e-13a3a9e97384}';
  17
+  let actual = uuid(firefoxUUID);
  18
+
  19
+  assert.equal(actual.number, firefoxUUID, 'uuid parsed given string');
  20
+  assert.equal(String(actual), firefoxUUID, 'serializes to the same value');
  21
+};
  22
+
15 23
 require('test').run(exports);
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.