MCPcopy Create free account
hub / github.com/itinance/react-native-fs / readFileGeneric

Function readFileGeneric

FS.common.js:147–175  ·  view source on GitHub ↗

* Generic function used by readFile and readFileAssets

(filepath: string, encodingOrOptions: ?string, command: Function)

Source from the content-addressed store, hash-verified

145 * Generic function used by readFile and readFileAssets
146 */
147function readFileGeneric(filepath: string, encodingOrOptions: ?string, command: Function) {
148 var options = {
149 encoding: 'utf8'
150 };
151
152 if (encodingOrOptions) {
153 if (typeof encodingOrOptions === 'string') {
154 options.encoding = encodingOrOptions;
155 } else if (typeof encodingOrOptions === 'object') {
156 options = encodingOrOptions;
157 }
158 }
159
160 return command(normalizeFilePath(filepath)).then((b64) => {
161 var contents;
162
163 if (options.encoding === 'utf8') {
164 contents = utf8.decode(base64.decode(b64));
165 } else if (options.encoding === 'ascii') {
166 contents = base64.decode(b64);
167 } else if (options.encoding === 'base64') {
168 contents = b64;
169 } else {
170 throw new Error('Invalid encoding type "' + String(options.encoding) + '"');
171 }
172
173 return contents;
174 });
175}
176
177/**
178 * Generic function used by readDir and readDirAssets

Callers 3

readFileFunction · 0.85
readFileAssetsFunction · 0.85
readFileResFunction · 0.85

Calls 1

normalizeFilePathFunction · 0.85

Tested by

no test coverage detected