<commit_before># grappling-hook
[![Build Status](https://travis-ci.org/keystonejs/grappling-hook.svg)](https://travis-ci.org/keystonejs/grappling-hook)
[![npm version](https://badge.fury.io/js/grappling-hook.svg)](http://npmjs.org/packages/grappling-hook)
[![Coverage Status](https://coveralls.io/repos/keystonejs/grappling-hook/badge.svg?branch=master)](https://coveralls.io/r/keystonejs/grappling-hook?branch=master)

>pre/post hooking enabler 

`grappling-hook` allows you to add pre/post hooks to objects and prototypes.
A number of modules already exist that allow you to do just the same, but the most popular one ([hooks](https://www.npmjs.com/package/hooks)) is no longer maintained.
Also, we wanted a more granular control of the hooking process and the way middleware is called.

**NEW:**
 
* since v2.4 you can [wrap sync methods](#adding-hooks-to-synchronized-methods) and [call sync hooks](#synchronized-hooks).
* since v2.3 you can [configure `grappling-hook` to use other method names](#other-qualifiers) than `pre` or `post`, e.g. `before` and `after`.

## Installation

```sh
$ npm install grappling-hook
```

## Usage


#### Creating a `grappling-hook` object

You can easily add methods to a new `grappling-hook` instance which are automatically ready for hooking up middleware:

```js
var grappling = require('grappling-hook');

var instance = grappling.create(); // create an instance

instance.addHooks({ // declare the hookable methods
	save: function(done){
		console.log('save!');
		!done && done();
	}
});

instance.pre('save', function(){ //allow middleware to be registered for a hook
	console.log('saving!');
}).post('save', function(){
	console.log('saved!');
});

```
```sh
# output:
saving!
save!
saved!
```

#### Using an existing object<commit_msg>Fix #10; example<commit_after># grappling-hook
[![Build Status](https://travis-ci.org/keystonejs/grappling-hook.svg)](https://travis-ci.org/keystonejs/grappling-hook)
[![npm version](https://badge.fury.io/js/grappling-hook.svg)](http://npmjs.org/packages/grappling-hook)
[![Coverage Status](https://coveralls.io/repos/keystonejs/grappling-hook/badge.svg?branch=master)](https://coveralls.io/r/keystonejs/grappling-hook?branch=master)

>pre/post hooking enabler 

`grappling-hook` allows you to add pre/post hooks to objects and prototypes.
A number of modules already exist that allow you to do just the same, but the most popular one ([hooks](https://www.npmjs.com/package/hooks)) is no longer maintained.
Also, we wanted a more granular control of the hooking process and the way middleware is called.

**NEW:**
 
* since v2.4 you can [wrap sync methods](#adding-hooks-to-synchronized-methods) and [call sync hooks](#synchronized-hooks).
* since v2.3 you can [configure `grappling-hook` to use other method names](#other-qualifiers) than `pre` or `post`, e.g. `before` and `after`.

## Installation

```sh
$ npm install grappling-hook
```

## Usage


#### Creating a `grappling-hook` object

You can easily add methods to a new `grappling-hook` instance which are automatically ready for hooking up middleware:

```js
var grappling = require('grappling-hook');

var instance = grappling.create(); // create an instance

instance.addHooks({ // declare the hookable methods
	save: function(done){
		console.log('save!');
		done();
	}
});

instance.pre('save', function(){ //allow middleware to be registered for a hook
	console.log('saving!');
}).post('save', function(){
	console.log('saved!');
});

instance.save(function(err){
  console.log('All done!!');
});
```
```sh
# output:
saving!
save!
saved!
All done!
```

#### Using an existing object
{'fraction_non_alphanumeric': 0.15757575757575756, 'fraction_numerical': 0.0027548209366391185, 'mean_word_length': 5.001652892561983, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 12, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3885158', 'n_tokens_mistral': 1224, 'n_tokens_neox': 1160, 'n_words': 335}