Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit 785be12

Browse files
committed
Increate prettier line length to 100
1 parent 1456130 commit 785be12

File tree

6 files changed

+48
-123
lines changed

6 files changed

+48
-123
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
},
3232
"homepage": "https://github.com/billmalarky/react-native-image-cache-hoc#readme",
3333
"prettier": {
34-
"singleQuote": true
34+
"singleQuote": true,
35+
"printWidth": 100
3536
},
3637
"jest": {
3738
"preset": "react-native",

src/FileSystem.js

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ export class FileSystem {
8686
*/
8787
_setBaseFilePath(fileDirName = null) {
8888
let baseFilePath =
89-
this.os == 'ios'
90-
? RNFetchBlob.fs.dirs.CacheDir
91-
: RNFetchBlob.fs.dirs.DocumentDir;
89+
this.os == 'ios' ? RNFetchBlob.fs.dirs.CacheDir : RNFetchBlob.fs.dirs.DocumentDir;
9290
baseFilePath += '/' + fileDirName + '/';
9391
return baseFilePath;
9492
}
@@ -108,9 +106,7 @@ export class FileSystem {
108106
* @private
109107
*/
110108
_validatePath(path, absolute = false) {
111-
let resolvedPath = absolute
112-
? pathLib.resolve(path)
113-
: pathLib.resolve(this.baseFilePath + path); // resolve turns any path into an absolute path (ie: /folder1/folder2/../example.js resolves to /folder1/example.js)
109+
let resolvedPath = absolute ? pathLib.resolve(path) : pathLib.resolve(this.baseFilePath + path); // resolve turns any path into an absolute path (ie: /folder1/folder2/../example.js resolves to /folder1/example.js)
114110

115111
if (resolvedPath.substr(0, this.baseFilePath.length) != this.baseFilePath) {
116112
throw new Error(resolvedPath + ' is not a valid file path.');
@@ -192,18 +188,13 @@ export class FileSystem {
192188
*/
193189
async fetchFile(url, permanent = false, fileName = null, clobber = false) {
194190
fileName = fileName || (await this.getFileNameFromUrl(url));
195-
let path =
196-
this.baseFilePath + (permanent ? 'permanent' : 'cache') + '/' + fileName;
191+
let path = this.baseFilePath + (permanent ? 'permanent' : 'cache') + '/' + fileName;
197192
this._validatePath(path, true);
198193

199194
// Clobber logic
200-
let fileExistsAtPath = await this.exists(
201-
(permanent ? 'permanent/' : 'cache/') + fileName
202-
);
195+
let fileExistsAtPath = await this.exists((permanent ? 'permanent/' : 'cache/') + fileName);
203196
if (!clobber && fileExistsAtPath) {
204-
throw new Error(
205-
'A file already exists at ' + path + ' and clobber is set to false.'
206-
);
197+
throw new Error('A file already exists at ' + path + ' and clobber is set to false.');
207198
}
208199

209200
// Logic here prunes cache directory on "cache" writes to ensure cache doesn't get too large.
@@ -269,9 +260,7 @@ export class FileSystem {
269260
this._validatePath('cache/' + blobStatObject.filename)
270261
) {
271262
overflowSize = overflowSize - parseInt(blobStatObject.size);
272-
RNFetchBlob.fs.unlink(
273-
this.baseFilePath + 'cache/' + blobStatObject.filename
274-
);
263+
RNFetchBlob.fs.unlink(this.baseFilePath + 'cache/' + blobStatObject.filename);
275264
}
276265
}
277266
}
@@ -300,10 +289,7 @@ export class FileSystem {
300289
*
301290
* @returns {FileSystem}
302291
*/
303-
export default function FileSystemFactory(
304-
cachePruneTriggerLimit = null,
305-
fileDirName = null
306-
) {
292+
export default function FileSystemFactory(cachePruneTriggerLimit = null, fileDirName = null) {
307293
if (!(this instanceof FileSystem)) {
308294
return new FileSystem(cachePruneTriggerLimit, fileDirName);
309295
}

src/imageCacheHoc.js

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,12 @@ import uuid from 'react-native-uuid';
2525
export default function imageCacheHoc(Image, options = {}) {
2626
// Validate options
2727
if (options.validProtocols && !Array.isArray(options.validProtocols)) {
28-
throw new Error(
29-
'validProtocols option must be an array of protocol strings.'
30-
);
28+
throw new Error('validProtocols option must be an array of protocol strings.');
3129
}
3230
if (options.fileHostWhitelist && !Array.isArray(options.fileHostWhitelist)) {
33-
throw new Error(
34-
'fileHostWhitelist option must be an array of host strings.'
35-
);
31+
throw new Error('fileHostWhitelist option must be an array of host strings.');
3632
}
37-
if (
38-
options.cachePruneTriggerLimit &&
39-
!Number.isInteger(options.cachePruneTriggerLimit)
40-
) {
33+
if (options.cachePruneTriggerLimit && !Number.isInteger(options.cachePruneTriggerLimit)) {
4134
throw new Error('cachePruneTriggerLimit option must be an integer.');
4235
}
4336
if (options.fileDirName && typeof options.fileDirName !== 'string') {
@@ -78,10 +71,7 @@ export default function imageCacheHoc(Image, options = {}) {
7871
*/
7972
static async cacheFile(url, permanent = false) {
8073
const fileSystem = FileSystemFactory();
81-
const localFilePath = await fileSystem.getLocalFilePathFromUrl(
82-
url,
83-
permanent
84-
);
74+
const localFilePath = await fileSystem.getLocalFilePathFromUrl(url, permanent);
8575

8676
return {
8777
url: url,
@@ -128,8 +118,7 @@ export default function imageCacheHoc(Image, options = {}) {
128118
this.options = {
129119
validProtocols: options.validProtocols || ['https'],
130120
fileHostWhitelist: options.fileHostWhitelist || [],
131-
cachePruneTriggerLimit:
132-
options.cachePruneTriggerLimit || 1024 * 1024 * 15, // Maximum size of image file cache in bytes before pruning occurs. Defaults to 15 MB.
121+
cachePruneTriggerLimit: options.cachePruneTriggerLimit || 1024 * 1024 * 15, // Maximum size of image file cache in bytes before pruning occurs. Defaults to 15 MB.
133122
fileDirName: options.fileDirName || null, // Namespace local file writing to this directory. Defaults to 'react-native-image-cache-hoc'.
134123
defaultPlaceholder: options.defaultPlaceholder || null, // Default placeholder component to render while remote image file is downloading. Can be overridden with placeholder prop. Defaults to <Image> component with style prop passed through.
135124
};
@@ -157,10 +146,7 @@ export default function imageCacheHoc(Image, options = {}) {
157146
// Validate source prop to be a valid web accessible url.
158147
if (
159148
!traverse(this.props).get(['source', 'uri']) ||
160-
!validator.isURL(
161-
traverse(this.props).get(['source', 'uri']),
162-
validatorUrlOptions
163-
)
149+
!validator.isURL(traverse(this.props).get(['source', 'uri']), validatorUrlOptions)
164150
) {
165151
throw new Error(
166152
'Invalid source prop. <CacheableImage> props.source.uri should be a web accessible url with a valid protocol and host. NOTE: Default valid protocol is https, default valid hosts are *.'
@@ -226,10 +212,7 @@ export default function imageCacheHoc(Image, options = {}) {
226212
const permanent = this.props.permanent ? true : false;
227213
let localFilePath = null;
228214
try {
229-
localFilePath = await this.fileSystem.getLocalFilePathFromUrl(
230-
url,
231-
permanent
232-
);
215+
localFilePath = await this.fileSystem.getLocalFilePathFromUrl(url, permanent);
233216
} catch (error) {
234217
console.warn(error); // eslint-disable-line no-console
235218
}
@@ -269,9 +252,7 @@ export default function imageCacheHoc(Image, options = {}) {
269252
if (this.state.localFilePath) {
270253
// Build platform specific file resource uri.
271254
const localFileUri =
272-
Platform.OS == 'ios'
273-
? this.state.localFilePath
274-
: 'file://' + this.state.localFilePath; // Android requires the traditional 3 prefixed slashes file:/// in a localhost absolute file uri.
255+
Platform.OS == 'ios' ? this.state.localFilePath : 'file://' + this.state.localFilePath; // Android requires the traditional 3 prefixed slashes file:/// in a localhost absolute file uri.
275256

276257
// Extract props proprietary to this HOC before passing props through.
277258
let { permanent, ...filteredProps } = this.props; // eslint-disable-line no-unused-vars
@@ -282,21 +263,13 @@ export default function imageCacheHoc(Image, options = {}) {
282263
return <Image {...props} />;
283264
} else {
284265
if (this.props.placeholder) {
285-
return (
286-
<this.props.placeholder.component
287-
{...this.props.placeholder.props}
288-
/>
289-
);
266+
return <this.props.placeholder.component {...this.props.placeholder.props} />;
290267
} else if (this.options.defaultPlaceholder) {
291268
return (
292-
<this.options.defaultPlaceholder.component
293-
{...this.options.defaultPlaceholder.props}
294-
/>
269+
<this.options.defaultPlaceholder.component {...this.options.defaultPlaceholder.props} />
295270
);
296271
} else {
297-
return (
298-
<Image style={this.props.style ? this.props.style : undefined} />
299-
);
272+
return <Image style={this.props.style ? this.props.style : undefined} />;
300273
}
301274
}
302275
}

tests/CacheableImage.test.js

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ describe('CacheableImage', function () {
4040
cachePruneTriggerLimit: 'string',
4141
});
4242
} catch (error) {
43-
error.should.deepEqual(
44-
new Error('cachePruneTriggerLimit option must be an integer.')
45-
);
43+
error.should.deepEqual(new Error('cachePruneTriggerLimit option must be an integer.'));
4644
}
4745

4846
try {
@@ -111,15 +109,13 @@ describe('CacheableImage', function () {
111109

112110
const CacheableImage = imageCacheHoc(Image);
113111

114-
return CacheableImage.cacheFile('https://i.redd.it/rc29s4bz61uz.png').then(
115-
(result) => {
116-
result.should.deepEqual({
117-
url: 'https://i.redd.it/rc29s4bz61uz.png',
118-
cacheType: 'cache',
119-
localFilePath: '/this/is/path/to/file.jpg',
120-
});
121-
}
122-
);
112+
return CacheableImage.cacheFile('https://i.redd.it/rc29s4bz61uz.png').then((result) => {
113+
result.should.deepEqual({
114+
url: 'https://i.redd.it/rc29s4bz61uz.png',
115+
cacheType: 'cache',
116+
localFilePath: '/this/is/path/to/file.jpg',
117+
});
118+
});
123119
});
124120

125121
it('#cacheFile static method should work as expected for permanent dir files.', () => {
@@ -135,10 +131,7 @@ describe('CacheableImage', function () {
135131

136132
const CacheableImage = imageCacheHoc(Image);
137133

138-
return CacheableImage.cacheFile(
139-
'https://i.redd.it/rc29s4bz61uz.png',
140-
true
141-
).then((result) => {
134+
return CacheableImage.cacheFile('https://i.redd.it/rc29s4bz61uz.png', true).then((result) => {
142135
result.should.deepEqual({
143136
url: 'https://i.redd.it/rc29s4bz61uz.png',
144137
cacheType: 'permanent',
@@ -167,9 +160,7 @@ describe('CacheableImage', function () {
167160
const cacheableImage = new CacheableImage(mockData.mockCacheableImageProps);
168161

169162
// Ensure defaults set correctly
170-
cacheableImage.props.should.have.properties(
171-
mockData.mockCacheableImageProps
172-
);
163+
cacheableImage.props.should.have.properties(mockData.mockCacheableImageProps);
173164
cacheableImage.state.should.have.properties({
174165
localFilePath: null,
175166
});
@@ -241,14 +232,12 @@ describe('CacheableImage', function () {
241232

242233
try {
243234
// eslint-disable-next-line no-unused-vars
244-
const cacheableImageWithProtocolOpts = new CacheableImageWithProtocolOpts(
245-
{
246-
source: {
247-
uri:
248-
'https://www.google.com/logos/doodles/2017/day-of-the-dead-2017-6241959625621504-l.png',
249-
},
250-
}
251-
);
235+
const cacheableImageWithProtocolOpts = new CacheableImageWithProtocolOpts({
236+
source: {
237+
uri:
238+
'https://www.google.com/logos/doodles/2017/day-of-the-dead-2017-6241959625621504-l.png',
239+
},
240+
});
252241

253242
throw new Error('Invalid source uri prop was accepted.');
254243
} catch (error) {
@@ -344,9 +333,7 @@ describe('CacheableImage', function () {
344333

345334
const CacheableImage = imageCacheHoc(Image);
346335

347-
const wrapper = shallow(
348-
<CacheableImage {...mockData.mockCacheableImageProps} />
349-
);
336+
const wrapper = shallow(<CacheableImage {...mockData.mockCacheableImageProps} />);
350337

351338
setImmediate(() => {
352339
expect(wrapper.prop('source')).toStrictEqual({
@@ -368,9 +355,7 @@ describe('CacheableImage', function () {
368355
it('#render with valid props does not throw an error.', (done) => {
369356
const CacheableImage = imageCacheHoc(Image);
370357

371-
const wrapper = shallow(
372-
<CacheableImage {...mockData.mockCacheableImageProps} />
373-
);
358+
const wrapper = shallow(<CacheableImage {...mockData.mockCacheableImageProps} />);
374359

375360
setImmediate(() => {
376361
expect(wrapper.prop('source')).toStrictEqual({

tests/FileSystem.test.js

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ describe('FileSystem', function () {
5959
let resolvedPath = pathLib.resolve(
6060
mockData.basePath + '/react-native-image-cache-hoc/' + badPath
6161
);
62-
error.should.deepEqual(
63-
new Error(resolvedPath + ' is not a valid file path.')
64-
);
62+
error.should.deepEqual(new Error(resolvedPath + ' is not a valid file path.'));
6563
}
6664

6765
let goodPath = 'safe/path';
@@ -147,9 +145,7 @@ describe('FileSystem', function () {
147145
const unknownFilename = await fileSystem.getFileNameFromUrl(
148146
'https://cdn2.hubspot.net/hub/42284/file-14233687-jpg/images/test_in_red'
149147
);
150-
unknownFilename.should.equal(
151-
'831eb245a3d9032cdce450f8760d2b8ddb442a3d.bin'
152-
);
148+
unknownFilename.should.equal('831eb245a3d9032cdce450f8760d2b8ddb442a3d.bin');
153149
});
154150

155151
it('#getLocalFilePathFromUrl should return local filepath if it exists on local fs in permanent dir.', () => {
@@ -227,13 +223,9 @@ describe('FileSystem', function () {
227223
})
228224
.catch((error) => {
229225
let resolvedPath = pathLib.resolve(
230-
mockData.basePath +
231-
'/react-native-image-cache-hoc/permanent/' +
232-
badFileName
233-
);
234-
error.should.deepEqual(
235-
new Error(resolvedPath + ' is not a valid file path.')
226+
mockData.basePath + '/react-native-image-cache-hoc/permanent/' + badFileName
236227
);
228+
error.should.deepEqual(new Error(resolvedPath + ' is not a valid file path.'));
237229
});
238230
});
239231

@@ -370,9 +362,7 @@ describe('FileSystem', function () {
370362
.catch((error) => {
371363
let resolvedPath = pathLib.resolve(mockData.basePath + badFileName);
372364

373-
error.should.deepEqual(
374-
new Error(resolvedPath + ' is not a valid file path.')
375-
);
365+
error.should.deepEqual(new Error(resolvedPath + ' is not a valid file path.'));
376366
});
377367
});
378368

tests/__mocks__/rn-fetch-blob.js

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,35 @@ module.exports = {
1212
let lstat = [
1313
{
1414
size: '43663',
15-
path:
16-
mockData.basePath +
17-
'/cache/0fbbfec764c73ee5b4e3a0cb8861469bc9fc6c8c.jpg',
15+
path: mockData.basePath + '/cache/0fbbfec764c73ee5b4e3a0cb8861469bc9fc6c8c.jpg',
1816
filename: '0fbbfec764c73ee5b4e3a0cb8861469bc9fc6c8c.jpg',
1917
lastModified: 1508878829000,
2018
type: 'file',
2119
},
2220
{
2321
size: '9000000',
24-
path:
25-
mockData.basePath +
26-
'/cache/6865fd0a65771b0044319f562873cc7b145abb4a.jpg',
22+
path: mockData.basePath + '/cache/6865fd0a65771b0044319f562873cc7b145abb4a.jpg',
2723
filename: '6865fd0a65771b0044319f562873cc7b145abb4a.jpg',
2824
lastModified: 1508877930000,
2925
type: 'file',
3026
},
3127
{
3228
size: '14133330',
33-
path:
34-
mockData.basePath +
35-
'/cache/b003269c377af6a2f53f59bc127a06c86f54312b.jpg',
29+
path: mockData.basePath + '/cache/b003269c377af6a2f53f59bc127a06c86f54312b.jpg',
3630
filename: 'b003269c377af6a2f53f59bc127a06c86f54312b.jpg',
3731
lastModified: 1508877698000,
3832
type: 'file',
3933
},
4034
{
4135
size: '1684',
42-
path:
43-
mockData.basePath +
44-
'/cache/d1052b9f22c1f00f4d658224f4295307b97db69f.jpg',
36+
path: mockData.basePath + '/cache/d1052b9f22c1f00f4d658224f4295307b97db69f.jpg',
4537
filename: 'd1052b9f22c1f00f4d658224f4295307b97db69f.jpg',
4638
lastModified: 1508877954000,
4739
type: 'file',
4840
},
4941
{
5042
size: '65769',
51-
path:
52-
mockData.basePath +
53-
'/cache/faf4e58257d988ea6eab23aee5e5733bff9b2a9e.jpg',
43+
path: mockData.basePath + '/cache/faf4e58257d988ea6eab23aee5e5733bff9b2a9e.jpg',
5444
filename: 'faf4e58257d988ea6eab23aee5e5733bff9b2a9e.jpg',
5545
lastModified: 1509634852000,
5646
type: 'file',

0 commit comments

Comments
 (0)