Skip to content

Commit

Permalink
Meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Feb 22, 2020
1 parent ad1707f commit c6f31c1
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 41 deletions.
18 changes: 3 additions & 15 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,11 @@ declare const clear: {
match(regex: RegExp): void;

/**
Clear one module from cache non-recursively. No parent or child modules should be affected.
Relevant for systems with a state that's dependant on Singelton pattern. When user would want to clear a specific module from memory without it having any "side effects" such as clearing a child module from memory as well.
@param moduleId - What you would use with `require()`.
@example
In the following example, `stats` module will **not** be cleared from memory
Clear a single module from the cache non-recursively. No parent or children modules will be affected.
**some-application-route.js**
```js
const stats = require('stats');
module.exports = () => {...}
```
This is mostly only useful if you use singletons, where you would want to clear a specific module without causing any side effects.
**code manager**
```js
clearModule.single('some-application-route');
```
@param moduleId - What you would use with `require()`.
*/
single(moduleId: string): void;
};
Expand Down
4 changes: 1 addition & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ const parentModule = require('parent-module');
const resolve = moduleId => {
try {
return resolveFrom(path.dirname(parentModule(__filename)), moduleId);
} catch (error) {
return undefined;
}
} catch (_) {}
};

const clear = moduleId => {
Expand Down
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

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:

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
"description": "Clear a module from the cache",
"license": "MIT",
"repository": "sindresorhus/clear-module",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"engines": {
"node": ">=8"
Expand Down
23 changes: 2 additions & 21 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
Useful for testing purposes when you need to freshly import a module.


## Install

```
$ npm install clear-module
```


## Usage

```js
Expand All @@ -35,7 +33,6 @@ require('./foo')();
//=> 1
```


## API

### clearModule(moduleId)
Expand All @@ -62,25 +59,9 @@ Regex to match against the module IDs.

### clearModule.single(moduleId)

Clear one module from cache non-recursively. No parent or child modules should be affected

<details>
<summary>Example use</summary>
Relevant for systems with a state that's dependant on Singelton pattern. When user would want to clear a specific module from memory without it having any "side effects" such as clearing a child module from memory as well.

In the following example, `stats` module will **not** be cleared from memory
Clear a single module from the cache non-recursively. No parent or children modules will be affected.

**some-application-route.js**
```js
const stats = require('stats');
module.exports = () => {...}
```

**code manager**
```js
clearModule.single('some-application-route');
```
</details>
This is mostly only useful if you use singletons, where you would want to clear a specific module without causing any side effects.

#### moduleId

Expand Down

0 comments on commit c6f31c1

Please sign in to comment.