Skip to content

Instantly share code, notes, and snippets.

Nicolas Chambrier naholyr

Block or report user

Report or block naholyr

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@naholyr
naholyr / _service.md
Created Dec 13, 2012
Sample /etc/init.d script
View _service.md

Sample service script for debianoids

Look at LSB init scripts for more information.

Usage

Copy to /etc/init.d:

# replace "$YOUR_SERVICE_NAME" with your service's name (whenever it's not enough obvious)
View 00-context.jsx
import * as React from 'react';
// TODO inject counters
const Counter = () => {
const [value, setValue] = React.useState(1);
const incr = React.useCallback(() => setValue(value + 1), [value]);
return (
<button type="button" onClick={incr}>
Click to incr. ({value})
@naholyr
naholyr / function-argnames.js
Created Feb 19, 2012
JS Introspection: extract function parameter names
View function-argnames.js
Function.prototype.argNames = function () {
// Extract function string representation: hopefully we can count on it ?
var s = this.toString();
// The cool thing is: this can only be a syntactically valid function declaration
s = s // "function name (a, b, c) { body }"
.substring( // "a, b, c"
s.indexOf('(')+1, // ----------------^
s.indexOf(')') // ------^
);
@naholyr
naholyr / examples-and-issues.js
Created Jan 22, 2012
Writing a "url_for" helper for Express
View examples-and-issues.js
// What name can I refer to ?
app.get('/hello-world', function () { console.log(3); });
// OK, suppose I can build the path from params
app.get('/:id/:name', function hello1 () { console.log(1); });
// OK, suppose the "callback" is the last one
app.get('/hello/:name', requiresAuthentication, function hello2 () { console.log(2); });
// How do I build the path ?
@naholyr
naholyr / 0.github-backed-comments.md
Last active Apr 18, 2019
Git(Hub)-backed comments system
View 0.github-backed-comments.md

General context

  • A static website using a generator (like Jekyll)
  • Comments are in a folder, one comment = one JSON file (attached to article based on path + filename)
  • website is versionned using git, repository hosted by github (cool for pull-requests)

Posting a new comment

OK here we need some dynamism ;)

View README.md

Shitty code, version 1:

const concat = s => s.reduce((r,s) => r + s)
const mult = (a, b) => a * b
const foo = a => b => c => concat([c, ' = ', mult(a, b)])
const bar = a => Number(a) + 1
const baz = b => b ? 42 : 33
const res = foo(1)('bar')('baz')
console.log(res)
@naholyr
naholyr / compare.php
Created Feb 22, 2012
Extract namespace from a PHP file
View compare.php
<?php
// Works in every situations
function by_token ($src) {
$tokens = token_get_all($src);
$count = count($tokens);
$i = 0;
$namespace = '';
$namespace_ok = false;
while ($i < $count) {
@naholyr
naholyr / monkey-patch.js
Created Dec 21, 2012
JS monkey patching
View monkey-patch.js
// Original method
var object = {
method: function (x, y) {
return x+y;
}
}
// Add operations before or after!
object.method = (function (original) {
return function (x, y) {
@naholyr
naholyr / _README.md
Last active Oct 16, 2018
Chrome extension exposing JS API
View _README.md

Install

Clone the repo and drop the folder into "chrome://extensions" page.

Use

Open any web page ("about:blank" will work too) and a console, then inspect and play with MY_API global variable.

@naholyr
naholyr / npm-check-used.sh
Created Apr 12, 2016
Check if your npm dependencies are actually used
View npm-check-used.sh
#!/bin/bash
# Run from your project's root
# Run "npm install" or "npm update" before
# Requirement: jq (https://stedolan.github.io/jq/)
required () {
grep -R "from ['\"]${1}[\\/'\"]\|require(['\"]${1}[\\/'\"]" --exclude-dir=node_modules --include='*.js' > /dev/null
}
You can’t perform that action at this time.