mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
8a49754cda
Summary: Fixes: https://github.com/facebook/react-native/issues/37135 - `substr()` is not part of the core JS since ~2018 - No wonder why no one noticed this :) - Though its supported by modern browsers, its deprecated - Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr ### Why `slice()` and not `substring()`? > Beacuse I like pizza ;) jk The reason, that I'm not using the most obvious alternative `substring()` is; - It _swaps the args_ passed, when; `startIndex > endIndex` —which I think is not a property of _good_ fn and also does not reflects the same in it's name. - It _doesn't support negative args_, which I think reduces flexibility to work with it. - It could lead to more bugs in most of the cases. ### Refecrences: - Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring#differences_between_substring_and_slice - Ref. for `slice()`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice - Ref. for `substring()`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring ## Changelog: [GENERAL][FIXED] - Refactor: `substr()` is deprecated, using `slice()` instead across RN codebase Pull Request resolved: https://github.com/facebook/react-native/pull/37136 Test Plan: - `yarn lint && yarn flow && yarn test-ci` --> _should be green_ Reviewed By: christophpurrer Differential Revision: D45477910 Pulled By: jacdebug fbshipit-source-id: 96a80893477599b9a549918924157627b9b0c3f4
93 lines
2.3 KiB
JavaScript
93 lines
2.3 KiB
JavaScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @format
|
|
* @flow strict
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
import type {PackagerAsset} from './registry.js';
|
|
|
|
const androidScaleSuffix = {
|
|
'0.75': 'ldpi',
|
|
'1': 'mdpi',
|
|
'1.5': 'hdpi',
|
|
'2': 'xhdpi',
|
|
'3': 'xxhdpi',
|
|
'4': 'xxxhdpi',
|
|
};
|
|
|
|
const ANDROID_BASE_DENSITY = 160;
|
|
|
|
/**
|
|
* FIXME: using number to represent discrete scale numbers is fragile in essence because of
|
|
* floating point numbers imprecision.
|
|
*/
|
|
function getAndroidAssetSuffix(scale: number): string {
|
|
if (scale.toString() in androidScaleSuffix) {
|
|
return androidScaleSuffix[scale.toString()];
|
|
}
|
|
// NOTE: Android Gradle Plugin does not fully support the nnndpi format.
|
|
// See https://issuetracker.google.com/issues/72884435
|
|
if (Number.isFinite(scale) && scale > 0) {
|
|
return Math.round(scale * ANDROID_BASE_DENSITY) + 'dpi';
|
|
}
|
|
throw new Error('no such scale ' + scale.toString());
|
|
}
|
|
|
|
// See https://developer.android.com/guide/topics/resources/drawable-resource.html
|
|
const drawableFileTypes = new Set([
|
|
'gif',
|
|
'jpeg',
|
|
'jpg',
|
|
'ktx',
|
|
'png',
|
|
'svg',
|
|
'webp',
|
|
'xml',
|
|
]);
|
|
|
|
function getAndroidResourceFolderName(
|
|
asset: PackagerAsset,
|
|
scale: number,
|
|
): string | $TEMPORARY$string<'raw'> {
|
|
if (!drawableFileTypes.has(asset.type)) {
|
|
return 'raw';
|
|
}
|
|
const suffix = getAndroidAssetSuffix(scale);
|
|
if (!suffix) {
|
|
throw new Error(
|
|
"Don't know which android drawable suffix to use for scale: " +
|
|
scale +
|
|
'\nAsset: ' +
|
|
JSON.stringify(asset, null, '\t') +
|
|
'\nPossible scales are:' +
|
|
JSON.stringify(androidScaleSuffix, null, '\t'),
|
|
);
|
|
}
|
|
return 'drawable-' + suffix;
|
|
}
|
|
|
|
function getAndroidResourceIdentifier(asset: PackagerAsset): string {
|
|
return (getBasePath(asset) + '/' + asset.name)
|
|
.toLowerCase()
|
|
.replace(/\//g, '_') // Encode folder structure in file name
|
|
.replace(/([^a-z0-9_])/g, '') // Remove illegal chars
|
|
.replace(/^assets_/, ''); // Remove "assets_" prefix
|
|
}
|
|
|
|
function getBasePath(asset: PackagerAsset): string {
|
|
const basePath = asset.httpServerLocation;
|
|
return basePath.startsWith('/') ? basePath.slice(1) : basePath;
|
|
}
|
|
|
|
module.exports = {
|
|
getAndroidResourceFolderName,
|
|
getAndroidResourceIdentifier,
|
|
getBasePath,
|
|
};
|