Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

doc-fn

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

doc-fn - npm Package Compare versions

Comparing version 0.1.0 to 0.1.1

5

package.json
{
"name": "doc-fn",
"description": "extracts documentation from functions for REPL and doc-compiling fun",
"version": "0.1.0",
"version": "0.1.1",
"main": "src/doc-fn.js",
"scripts": {
"test": "mocha --ui qunit"
"test": "mocha --ui qunit",
"prepublish": "npm test"
},

@@ -9,0 +10,0 @@ "repository": {

17

src/doc-fn.js
var doc_comment = /^function\s+[^{]+{((?:\s*\/{3}.+)+)/
var doc_surroundings = /(^\s*\/{3}\s?)|(\s+$)/gm
var section = /.+:.+/g
var sectionGroups = /(.+):(.+)/

@@ -10,7 +12,20 @@ var getDoc = function(fn) {

var getDocs = function(docStr){
var sections = docStr.match(section)
if ( !sections ) return {}
return sections.reduce(function(docs, section){
var s = sectionGroups.exec(section)
docs[s[1].trim()] = s[2].trim()
return docs
}, {})
}
var doc = function(fn){
/// signature: function -> function
/// mutates a function to add a doc property, which includes all comments starting with `///`
/// at the top of a function
/// at the top of a function.
/// also adds a .docs property, containing a map of 'section names' to 'vals', for every line
/// with the format '/// section name: value'
fn.doc = getDoc(fn)
if ( fn.doc ) fn.docs = getDocs(fn.doc)
return fn

@@ -17,0 +32,0 @@ }

@@ -22,3 +22,3 @@ var a = require('assert'),

test('it should only consider /// comments at the beginning of the function body.', function() {
var k = function(a) {
var k = doc(function(a) {
/// The constant function.

@@ -29,4 +29,4 @@ function _K() {

}
}
doc(k)
})
a.equal(k.doc, 'The constant function.')

@@ -36,11 +36,22 @@ })

test('it should be null', function(){
var inc = function(a){
test('.doc should be null if there are no ///s', function(){
var inc = doc(function(a){
return a + 1
}
})
doc(inc)
a.equal(inc.doc, null)
})
test('.docs holds normal docs in a body, and foo: bar in .foo', function(){
var inc = doc(function(a){
/// signature: a -> a
/// description: increments a number.
return a + 1
})
a.equal(inc.docs.signature, 'a -> a')
a.equal(inc.docs.description, 'increments a number.')
a.equal(inc.doc, 'signature: a -> a\ndescription: increments a number.')
})
suite('merge')

@@ -47,0 +58,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc