Skip to content

Commit

Permalink
refactor, add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonschlinkert committed May 14, 2022
1 parent 4245348 commit de9de56
Show file tree
Hide file tree
Showing 34 changed files with 617 additions and 475 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[{**/{actual,fixtures,expected,templates}/**,*.md}]
[{test/fixtures/**,templates/**}]
trim_trailing_whitespace = false
insert_final_newline = false
44 changes: 19 additions & 25 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,17 @@
],

"env": {
"browser": false,
"es6": true,
"node": true,
"mocha": true
"es2021": true,
"node": true
},

"parserOptions":{
"ecmaVersion": 9,
"sourceType": "module",
"ecmaFeatures": {
"modules": true,
"experimentalObjectRestSpread": true
}
},

"globals": {
"document": false,
"navigator": false,
"window": false
"parserOptions": {
"ecmaVersion": 12
},

"rules": {
"accessor-pairs": 2,
"arrow-parens": [2, "as-needed"],
"arrow-spacing": [2, { "before": true, "after": true }],
"block-spacing": [2, "always"],
"brace-style": [2, "1tbs", { "allowSingleLine": true }],
Expand All @@ -39,7 +27,7 @@
"eol-last": 2,
"eqeqeq": [2, "allow-null"],
"generator-star-spacing": [2, { "before": true, "after": true }],
"handle-callback-err": [2, "^(err|error)$" ],
"handle-callback-err": [2, "^(err|error)$"],
"indent": [2, 2, { "SwitchCase": 1 }],
"key-spacing": [2, { "beforeColon": false, "afterColon": true }],
"keyword-spacing": [2, { "before": true, "after": true }],
Expand Down Expand Up @@ -68,18 +56,20 @@
"no-floating-decimal": 2,
"no-func-assign": 2,
"no-implied-eval": 2,
"no-implicit-coercion": 2,
"no-inner-declarations": [2, "functions"],
"no-invalid-regexp": 2,
"no-irregular-whitespace": 2,
"no-iterator": 2,
"no-label-var": 2,
"no-labels": 2,
"no-lone-blocks": 2,
"no-lonely-if": 2,
"no-mixed-spaces-and-tabs": 2,
"no-multi-spaces": 2,
"no-multi-spaces": 0,
"no-multi-str": 2,
"no-multiple-empty-lines": [2, { "max": 1 }],
"no-native-reassign": 0,
"no-native-reassign": 2,
"no-negated-in-lhs": 2,
"no-new": 2,
"no-new-func": 2,
Expand All @@ -89,7 +79,7 @@
"no-obj-calls": 2,
"no-octal": 2,
"no-octal-escape": 2,
"no-proto": 0,
"no-proto": 2,
"no-redeclare": 2,
"no-regex-spaces": 2,
"no-return-assign": 2,
Expand All @@ -100,28 +90,32 @@
"no-sparse-arrays": 2,
"no-this-before-super": 2,
"no-throw-literal": 2,
"no-trailing-spaces": 0,
"no-trailing-spaces": 2,
"no-undef": 2,
"no-undef-init": 2,
"no-unexpected-multiline": 2,
"no-unneeded-ternary": [2, { "defaultAssignment": false }],
"no-unreachable": 2,
"no-unused-expressions": 2,
"no-unused-vars": [2, { "vars": "all", "args": "none" }],
"no-useless-call": 0,
"no-useless-call": 2,
"no-with": 2,
"one-var": [0, { "initialized": "never" }],
"object-curly-spacing": ["error", "always", { "objectsInObjects": true }],
"one-var": [2, { "initialized": "never" }],
"operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }],
"padded-blocks": [0, "never"],
"prefer-const": [2, { "destructuring": "all", "ignoreReadBeforeAssign": false }],
"quotes": [2, "single", "avoid-escape"],
"radix": 2,
"semi": [2, "always"],
"semi-spacing": [2, { "before": false, "after": true }],
"space-before-blocks": [2, "always"],
"space-before-function-paren": [2, "never"],
"space-before-function-paren": [2, { "anonymous": "never", "named": "never", "asyncArrow": "always" }],
"space-in-parens": [2, "never"],
"space-infix-ops": 2,
"space-unary-ops": [2, { "words": true, "nonwords": false }],
"spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }],
"strict": 2,
"use-isnan": 2,
"valid-typeof": 2,
"wrap-iife": [2, "any"],
Expand Down
8 changes: 0 additions & 8 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
# Enforce Unix newlines
* text eol=lf

# binaries
*.ai binary
*.psd binary
*.jpg binary
*.gif binary
*.png binary
*.jpeg binary
1 change: 1 addition & 0 deletions .github/funding.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: jonschlinkert
23 changes: 23 additions & 0 deletions .github/workflow/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Tests
on: [push, pull_request]

env:
CI: true

jobs:
test:
name: Node.js ${{ matrix.node-version }} @ ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node-version: [10, 12, 13, 14, 15]

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

# test related, or directories generated by tests
test/actual
!test/actual/foo.js
actual
coverage
.nyc*
Expand All @@ -19,6 +20,7 @@ yarn-error.log

# Text editors
*.sublime-*
*.code-*

# custom
_gh_pages
Expand All @@ -30,3 +32,4 @@ temp
tmp
TODO.md
package-lock.json
attempts
99 changes: 30 additions & 69 deletions .verb.md
Original file line number Diff line number Diff line change
@@ -1,101 +1,62 @@
## Heads up!

### Breaking changes in v1.0!

Prior to v1.0, this library also attempted to convert the returned patterns into valid globs.

As of v1.0, parse-gitignore was refactored and simplified down to less than ~50 sloc and no dependencies to provide a quick and easy way of getting the array of ignore patterns from a [.gitignore]() file. This allows you to do whatever you need to do with the patterns.

## Usage


```js
const fs = require('fs');
const gitignore = require('{%= name %}');

// pass the contents of a .gitignore file as a string or buffer
console.log(gitignore(fs.readFileSync('foo/bar/.gitignore')));
//=> ['*.DS_Store', 'node_modules', ...];
// Pass a string, buffer, or file path to gitignore()
const contents = fs.readFileSync('.gitignore');
const { patterns, sections } = gitignore(contents);
```

## Example

Parses the contents of a `.gitignore` file, like the folowing:

```sh
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock
```

Into an array, like the following:

```js
[ 'logs',
'*.log',
'npm-debug.log*',
'yarn-debug.log*',
'yarn-error.log*',
'pids',
'*.pid',
'*.seed',
'*.pid.lock' ]
```

## .parse

Given the following contents in `foo/bar/.gitignore`:

```
const parsed = gitignore(`
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
# Runtime data
pids
*.pid
*.seed
*.pid.lock
```

Results in something like the following:
*.seed
`);

```js
const parsed = gitignore.parse(fs.readFileSync('foo/bar/.gitignore'));
//
// results in the following
//
//

{
patterns: [
'logs',
'*.log',
'npm-debug.log*',
'yarn-debug.log*',
'yarn-error.log*',
'pids',
'*.pid',
'*.seed',
'*.pid.lock'
],
patterns: ['logs', '*.log', 'pids', '*.pid', '*.pid.lock', '*.seed'],
sections: [
{
name: 'Logs',
patterns: ['logs', '*.log', 'npm-debug.log*', 'yarn-debug.log*', 'yarn-error.log*']
value: '# Logs',
patterns: [ 'logs', '*.log']
},
{
name: 'Runtime data',
patterns: ['pids', '*.pid', '*.seed', '*.pid.lock']
value: '# Runtime data',
patterns: [ 'pids', '*.pid', '*.pid.lock', '*.seed' ]
}
]
}
```
```

## Release history

### v2.0




### v1.0

**Heads up! Breaking changes!**

Prior to v1.0, this library also attempted to convert the returned patterns into valid globs.

As of v1.0, parse-gitignore was refactored and simplified down to less than ~50 sloc and no dependencies to provide a quick and easy way of getting the array of ignore patterns from a [.gitignore]() file. This allows you to do whatever you need to do with the patterns.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ As of v1.0, parse-gitignore was refactored and simplified down to less than ~50
const fs = require('fs');
const parse = require('parse-gitignore');

// pass the contents of a .gitignore file as a string or buffer
// pass the contents of a .gitignore file as a string or buffer
console.log(parse(fs.readFileSync('foo/bar/.gitignore')));
//=> ['*.DS_Store', 'node_modules', ...];
```
Expand Down Expand Up @@ -107,7 +107,7 @@ You might also be interested in these projects:

### Contributors

| **Commits** | **Contributor** |
| **Commits** | **Contributor** |
| --- | --- |
| 33 | [jonschlinkert](https://github.com/jonschlinkert) |
| 1 | [schnittstabil](https://github.com/schnittstabil) |
Expand All @@ -128,4 +128,4 @@ Released under the [MIT License](LICENSE).

***

_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on July 26, 2018._
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on July 26, 2018._

0 comments on commit de9de56

Please sign in to comment.