Skip to content

Instantly share code, notes, and snippets.

@djedr
djedr / 2017-02-01.md
Last active December 28, 2023 15:23
Combining the C preprocessor and JavaScript

Note

This text is superseded by https://gist.github.com/djedr/7d21eac05ce2bbbca29b29d532a1fbe4.

Why

There are some reasons listed here. To expand on that, using the C preprocessor with JavaScript project also gives us an easy way to share constants between different files. Back-end (Node.js) and front-end (Angular, React). But you can also share constants between .js files and .css files or any other text formats. No fuss. Just #include "constants.cpp".

Macros that operate on plain text are more limited and sloppier than macros that operate on syntax trees, like sweet.js. But they are also simpler. And very often enough.

Also:

  • cpp is simple
@djedr
djedr / lisperator-problem-extended-25-11-2016.js
Last active May 29, 2019 19:38
My solution to "A little JavaScript problem" from lisperator.net [ http://lisperator.net/blog/a-little-javascript-problem/ ], with comments.
/**
* @file Solution to "A little JavaScript problem" from [lisperator.net]{@link http://lisperator.net/blog/a-little-javascript-problem/}.
*
* For demonstration. Overdocumented on purpose. Documentation is in a JSDoc-like documentation style.
* The solution itself is 7 SLOC.
* I also included test code to automatically verify correctness of the solution.
* See {@link https://gist.github.com/djedr/68fdaef3cad134788caefac06388534c} for a version without documentation or test code.
*
* Assumes ES6 support.
* Uses nested closures to emulate singly linked lists.