[Pkg-javascript-commits] [node-asn1.js] 17/202: readme: example

Bastien Roucariès rouca at moszumanska.debian.org
Thu Apr 20 19:18:50 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-asn1.js.

commit 7562fe133f151e869aa9d09d84683a4f566b65f4
Author: Fedor Indutny <fedor.indutny at gmail.com>
Date:   Mon Dec 2 00:08:46 2013 +0400

    readme: example
---
 README.md | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)

diff --git a/README.md b/README.md
index c64e686..0e1324a 100644
--- a/README.md
+++ b/README.md
@@ -2,6 +2,64 @@
 
 ASN.1 DER Encoder/Decoder and DSL.
 
+## Example
+
+Define model:
+
+```javascript
+var asn = require('asn1.js');
+
+var Human = asn.define('Human', function() {
+  this.seq().obj(
+    this.key('firstName').octstr(),
+    this.key('lastName').octstr(),
+    this.key('age').int(),
+    this.key('gender').enum({ 0: 'male', 1: 'female' }),
+    this.key('bio').seqof(Bio)
+  );
+});
+
+var Bio = asn.define('Bio', function() {
+  this.seq().obj(
+    this.key('time').gentime(),
+    this.key('description').octstr()
+  );
+});
+```
+
+Encode data:
+
+```javascript
+var output = Human.encode({
+  firstName: 'Thomas',
+  lastName: 'Anderson',
+  age: 30, // Perhaps?!
+  gender: 'male',
+  bio: [
+    {
+      time: +new Date('31 March 1999'),
+      description: 'freedom of mind'
+    }
+  ]
+}, 'der');
+```
+
+Decode data:
+
+```javascript
+var human = Human.decode(output, 'der');
+console.log(human);
+/*
+{ firstName: <Buffer 54 68 6f 6d 61 73>,
+  lastName: <Buffer 41 6e 64 65 72 73 6f 6e>,
+  age: 30,
+  gender: 'male',
+  bio:
+   [ { time: 922820400000,
+       description: <Buffer 66 72 65 65 64 6f 6d 20 6f 66 20 6d 69 6e 64> } ] }
+*/
+```
+
 #### LICENSE
 
 This software is licensed under the MIT License.

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-asn1.js.git



More information about the Pkg-javascript-commits mailing list