All Projects → bahmutov → comment-value

bahmutov / comment-value

Licence: other
Instruments a Node program and updates its comments with computed expression values

Programming Languages

javascript
184084 projects - #8 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to comment-value

Dictionary
A dictionary data type with a fast b-tree based search
Stars: ✭ 39 (+44.44%)
Mutual labels:  value
laravel-cache
An improved helper for working with cache
Stars: ✭ 64 (+137.04%)
Mutual labels:  helper
hcv-color
🌈 Color model HCV/HCG is an alternative to HSV and HSL, derived by Munsell color system, usable for Dark and Light themes... 🌈
Stars: ✭ 44 (+62.96%)
Mutual labels:  value
helpers.js
Small JavaScript functions for common use cases.
Stars: ✭ 37 (+37.04%)
Mutual labels:  helper
Android-SharedPreferences-Helper
This Shared Preferences Helper library (Library size = ~15kb only) Simplifies usage of the default Android SharedPreferences Class. The developer can do in a few lines of code which otherwise would have required several. Simple to understand as compared to the default Shared Preferences class and easy to use. Can be used by simply adding the dep…
Stars: ✭ 15 (-44.44%)
Mutual labels:  helper
jasper-helpers
A library of helpers for working with html for apps Crystal
Stars: ✭ 20 (-25.93%)
Mutual labels:  helper
libimobiledevice-glue
A library with common code used by libraries and tools around the libimobiledevice project
Stars: ✭ 46 (+70.37%)
Mutual labels:  helper
html-comment-regex
Regular expression for matching HTML comments
Stars: ✭ 15 (-44.44%)
Mutual labels:  comment
SQLServerTools
This repo is the home of various SQL-Server-Tools
Stars: ✭ 28 (+3.7%)
Mutual labels:  helper
Country
Country gives a list of countries with all their flags.
Stars: ✭ 26 (-3.7%)
Mutual labels:  helper
use-color-change
📈📉React hook for flashing a text when a value becomes higher or lower
Stars: ✭ 32 (+18.52%)
Mutual labels:  value
langx-java
Java tools, helper, common utilities. A replacement of guava, apache-commons, hutool
Stars: ✭ 50 (+85.19%)
Mutual labels:  helper
background-service-lib
Essential classes for reliable background services.
Stars: ✭ 24 (-11.11%)
Mutual labels:  helper
NFlags
Simple yet powerfull library to made parsing CLI arguments easy. Library also allow to print usage help "out of box".
Stars: ✭ 44 (+62.96%)
Mutual labels:  helper
vkhelpers
Vulkan c helper library
Stars: ✭ 25 (-7.41%)
Mutual labels:  helper
has-value
Returns true if a value exists, false if empty. Works with deeply nested values using object paths.
Stars: ✭ 27 (+0%)
Mutual labels:  value
prodoc.nvim
a neovim comment and annotation plugin using coroutine
Stars: ✭ 47 (+74.07%)
Mutual labels:  comment
raise if
one liner `raise Exception if condition` for Python
Stars: ✭ 15 (-44.44%)
Mutual labels:  helper
phoenix html simplified helpers
Some helpers for phoenix html( truncate, time_ago_in_words, number_with_delimiter, url_for, current_page? )
Stars: ✭ 29 (+7.41%)
Mutual labels:  helper
XcodeCommentWrapper
Xcode extension for wrapping comments
Stars: ✭ 29 (+7.41%)
Mutual labels:  comment

comment-value

Instruments a Node program and updates its comments with computed expression values

NPM

Build status semantic-release js-standard-style

Why?

Writing and maintaining code examples is hard. Often the values shown in the comments (think blog posts) are out of date and incorrect.

Read more about the problem and this tool solves it in blog post Accurate values in comments.

What does comment-value do?

comment-value (or available aliases values, comment, comments) executes your Node program, instrumenting it on the fly. Every time it sees a special comment that starts with //>, it will set it value from whatever the expression immediately to its left is.

When Node finishes, the file is saved back with updated comments.

comment-value in action

Click on the above screen shot to see 15 second demo clip.

Watch mode

Automatically (well, as long as chokidar works) reruns and updates comments in the source file. See the video, it is awesome!

Install and use

Install comment-value either as a global or as a local package.

npm install -g comment-value

Use either using node -r comment-value index.js or via CLI alias: comment-value, values or cv like this values index.js.

Alias values is the preferred way. It allows

  • Watch files for changes and rerun with -w, --watch option
  • Print instrumented file with -i, --instrumented option

Example

Add a few comments that start with //> to index.js. You can put anything after that into the comment.

// index.js
const add = (a, b) => a + b
add(2, 3) //>
add(2, -3) //> ? anything here
// you can also print variables
const t = typeof add
// t:
// or variable types directly
// add::

Run the comment-value script which runs your Node

$ comment-value index.js

The index.js will now contain

// index.js
const add = (a, b) => a + b
add(2, 3) //> 5
add(2, -3) //> -1
// you can also print variables!
const t = typeof add
// t: "function"
// add:: "function"

Comment format

You can start special value comments to be updated with strings //>, //=>, //~>, // >, // => and even // ~>.

For variables, use line comment with just variable name followed by :, for example

function add(a, b) {
  // a:
  // b:
}
add(10, 2)

which will produce

function add(a, b) {
  // a: 10
  // b: 2
}
add(10, 2)

To print the type of a variable, use variable name followed by ::, for example

function add(a, b) {
  // a:: "number"
  // b:: "string"
}
add(10, 'foo')

Composed functions

You can even get results from composed functions, for example, the values below were all computed automatically

var R = require('ramda')
R.compose(
  Math.abs,     //=> 7
  R.add(1),     //=> -7
  R.multiply(2) //=> -8
)(-4) //=> 7

Mixing values and types

You can record either values or types of values

var R = require('ramda')
R.compose(
  Math.abs,     //:: number
  R.add(1),     //=> -7
  R.multiply(2) //=> -8
)(-4)
// :: number

Console log statements

If the value comment is on the left of console.log(value) expression, then it will be updated with the value.

// index.js
console.log(2 + 40) //> ?
$ comment-value index.js
42
// index.js
console.log(2 + 40) //> 42

Debug

To see verbose messages while this module runs, set the environment variable DEBUG before running

DEBUG=comment-value node ...

The instrumenting function has a global emitter, you can receive messages when special comments are found and when an expression is wrapped. For example this code will produce the following events

// index.js
console.log(2 + 40) //> ??
// spec.js
let emitter
beforeEach(() => {
  emitter = global.instrument
})
it('finds the comment', () => {
  const comments = []
  const wrapped = []
  emitter.on('comment', c => comments.push(c))
  emitter.on('wrap', w => wrapped.push(w))
  instrument(source)
  // comments will be ["> ??"]
  // wrapped will be ["2 + 40"]
})

This is an internal feature and is used during unit tests.

Small print

Author: Gleb Bahmutov <[email protected]> © 2017

License: MIT - do anything with the code, but don't blame me if it does not work.

Support: if you find any problems with this module, email / tweet / open issue on Github

MIT License

Copyright (c) 2017 Gleb Bahmutov <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].