Skip to content

Commit

Permalink
format all files with prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisblossom committed May 12, 2018
1 parent 55ebf93 commit 19091ad
Show file tree
Hide file tree
Showing 4 changed files with 538 additions and 712 deletions.
26 changes: 13 additions & 13 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ orientation.
Examples of behavior that contributes to creating a positive environment
include:

* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members
* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members

Examples of unacceptable behavior by participants include:

* The use of sexualized language or imagery and unwelcome sexual attention or
advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic
address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting
* The use of sexualized language or imagery and unwelcome sexual attention or
advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic
address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting

## Our Responsibilities

Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,23 @@ Add this to your `package.json` Jest config:
Or include only in individual tests:

```js
const serializer = require('jest-serializer-path')
const serializer = require("jest-serializer-path");

expect.addSnapshotSerializer(serializer)
expect.addSnapshotSerializer(serializer);
```

All absolute paths will now be converted and saved in snapshots like so:

`/path/to/my-proj/lib` => `<PROJECT_ROOT>/lib`

``/path/to/os-temp/nested/temp`` => ``<TEMP_DIR>/nested/temp``
`/path/to/os-temp/nested/temp` => `<TEMP_DIR>/nested/temp`

``/path/to/user-home/nested/home`` => ``<HOME_DIR>/nested/home``
`/path/to/user-home/nested/home` => `<HOME_DIR>/nested/home`

#### Caveats

* All single backslashes (`\`) will be replaced by a forward slash (`/`).
* Any string that looks like a Windows drive letter (`C:\`) will be replaced by a forward slash (`/`).
* All single backslashes (`\`) will be replaced by a forward slash (`/`).
* Any string that looks like a Windows drive letter (`C:\`) will be replaced by a forward slash (`/`).

#### Build

Expand Down
208 changes: 85 additions & 123 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,196 +1,158 @@
'use strict'
"use strict";

const slash = require('slash')
const cloneDeep = require('lodash.clonedeep')
const isError = require('lodash.iserror')
const os = require('os')
const path = require('path')
const fs = require('fs')
const slash = require("slash");
const cloneDeep = require("lodash.clonedeep");
const isError = require("lodash.iserror");
const os = require("os");
const path = require("path");
const fs = require("fs");

module.exports = {
print (val, serialize) {

let normalizedValue = val
print(val, serialize) {
let normalizedValue = val;

if (isError(normalizedValue)) {

const message = normalizePaths(normalizedValue.message)
const error = new Error(message)
const message = normalizePaths(normalizedValue.message);
const error = new Error(message);

// Clone hidden props
const ownProps = Object.getOwnPropertyNames(error)
const ownProps = Object.getOwnPropertyNames(error);
for (const index in ownProps) {

// istanbul ignore else hasOwnProperty will never be tested
if (Object.prototype.hasOwnProperty.call(ownProps, index)) {
const key = ownProps[index];

const key = ownProps[index]

error[key] = normalizePaths(normalizedValue[key])

error[key] = normalizePaths(normalizedValue[key]);
}

}

// Clone normal props
for (const index in normalizedValue) {

// istanbul ignore else hasOwnProperty will never be tested
if (Object.prototype.hasOwnProperty.call(normalizedValue, index)) {

error[index] = normalizePaths(normalizedValue[index])

error[index] = normalizePaths(normalizedValue[index]);
}

}

normalizedValue = error

}
else if (typeof normalizedValue === 'object') {

normalizedValue = cloneDeep(normalizedValue)
normalizedValue = error;
} else if (typeof normalizedValue === "object") {
normalizedValue = cloneDeep(normalizedValue);

Object.keys(normalizedValue).forEach(key => {

normalizedValue[key] = normalizePaths(normalizedValue[key])

})

normalizedValue[key] = normalizePaths(normalizedValue[key]);
});
} else {
normalizedValue = normalizePaths(normalizedValue);
}
else {

normalizedValue = normalizePaths(normalizedValue)

}

return serialize(normalizedValue)

return serialize(normalizedValue);
},
test (val) {

let has = false

if (val && typeof val === 'object') {
test(val) {
let has = false;

if (val && typeof val === "object") {
// val.message is non-enumerable in an error
if (val.message && shouldUpdate(val.message)) {

has = true

has = true;
}

Object.keys(val).forEach(key => {

if (shouldUpdate(val[key])) {

has = true

has = true;
}

})

});
} else if (shouldUpdate(val)) {
has = true;
}
else if (shouldUpdate(val)) {

has = true

}

return has

return has;
},
normalizePaths,
getRealPath,
}
getRealPath
};

/**
* Normalize paths across platforms.
* Filters must be ran on all platforms to guard against false positives
*/
function normalizePaths (value) {

if (typeof value !== 'string') {

return value

function normalizePaths(value) {
if (typeof value !== "string") {
return value;
}

const cwd = process.cwd()
const cwdReal = getRealPath(cwd)
const tempDir = os.tmpdir()
const tempDirReal = getRealPath(tempDir)
const homeDir = os.homedir()
const homeDirReal = getRealPath(homeDir)
const cwd = process.cwd();
const cwdReal = getRealPath(cwd);
const tempDir = os.tmpdir();
const tempDirReal = getRealPath(tempDir);
const homeDir = os.homedir();
const homeDirReal = getRealPath(homeDir);

const homeRelativeToTemp = path.relative(tempDir, homeDir)
const homeRelativeToTempReal = path.relative(tempDirReal, homeDir)
const homeRealRelativeToTempReal = path.relative(tempDirReal, homeDirReal)
const homeRealRelativeToTemp = path.relative(tempDir, homeDirReal)
const homeRelativeToTemp = path.relative(tempDir, homeDir);
const homeRelativeToTempReal = path.relative(tempDirReal, homeDir);
const homeRealRelativeToTempReal = path.relative(tempDirReal, homeDirReal);
const homeRealRelativeToTemp = path.relative(tempDir, homeDirReal);

const runner = [
// Replace process.cwd with <PROJECT_ROOT>
val => val.split(cwdReal).join('<PROJECT_ROOT>'),
val => val.split(cwd).join('<PROJECT_ROOT>'),
val => val.split(cwdReal).join("<PROJECT_ROOT>"),
val => val.split(cwd).join("<PROJECT_ROOT>"),

// Replace home directory with <TEMP_DIR>
val => val.split(tempDirReal).join('<TEMP_DIR>'),
val => val.split(tempDir).join('<TEMP_DIR>'),
val => val.split(tempDirReal).join("<TEMP_DIR>"),
val => val.split(tempDir).join("<TEMP_DIR>"),

// Replace home directory with <HOME_DIR>
val => val.split(homeDirReal).join('<HOME_DIR>'),
val => val.split(homeDir).join('<HOME_DIR>'),
val => val.split(homeDirReal).join("<HOME_DIR>"),
val => val.split(homeDir).join("<HOME_DIR>"),

// handle HOME_DIR nested inside TEMP_DIR
val => val.split(`<TEMP_DIR>${path.sep + homeRelativeToTemp}`).join('<HOME_DIR>'),
val => val.split(`<TEMP_DIR>${path.sep + homeRelativeToTempReal}`).join('<HOME_DIR>'), // untested
val => val.split(`<TEMP_DIR>${path.sep + homeRealRelativeToTempReal}`).join('<HOME_DIR>'),
val => val.split(`<TEMP_DIR>${path.sep + homeRealRelativeToTemp}`).join('<HOME_DIR>'), // untested
val =>
val
.split(`<TEMP_DIR>${path.sep + homeRelativeToTemp}`)
.join("<HOME_DIR>"),
val =>
val
.split(`<TEMP_DIR>${path.sep + homeRelativeToTempReal}`)
.join("<HOME_DIR>"), // untested
val =>
val
.split(`<TEMP_DIR>${path.sep + homeRealRelativeToTempReal}`)
.join("<HOME_DIR>"),
val =>
val
.split(`<TEMP_DIR>${path.sep + homeRealRelativeToTemp}`)
.join("<HOME_DIR>"), // untested

// Remove win32 drive letters, C:\ -> \
val => val.replace(/[a-zA-Z]:\\/g, '\\'),
val => val.replace(/[a-zA-Z]:\\/g, "\\"),

// Convert win32 backslash's to forward slashes, \ -> /
val => slash(val),
]
val => slash(val)
];

let result = value
let result = value;
runner.forEach(current => {
result = current(result);
});

result = current(result)

})

return result

return result;
}

function shouldUpdate (value) {

if (typeof value !== 'string') {

return false

function shouldUpdate(value) {
if (typeof value !== "string") {
return false;
}

// return true if value is different from normalized value
return normalizePaths(value) !== value

return normalizePaths(value) !== value;
}

function getRealPath (pathname) {

function getRealPath(pathname) {
try {
const realPath = fs.realpathSync(pathname);

const realPath = fs.realpathSync(pathname)

return realPath

}
catch (error) {

return pathname

return realPath;
} catch (error) {
return pathname;
}

}
Loading

0 comments on commit 19091ad

Please sign in to comment.