Skip to content

Commit

Permalink
refactor: replace shortid with nanoid. closes react-navigation#7858
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Mar 30, 2020
1 parent 4ac40b5 commit 9bb5cfd
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 47 deletions.
3 changes: 1 addition & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,14 @@
"@react-navigation/routers": "^5.2.0",
"escape-string-regexp": "^2.0.0",
"query-string": "^6.11.1",
"nanoid": "^3.0.2",
"react-is": "^16.13.0",
"shortid": "^2.2.15",
"use-subscription": "^1.4.0"
},
"devDependencies": {
"@react-native-community/bob": "^0.10.0",
"@types/react": "^16.9.23",
"@types/react-is": "^16.7.1",
"@types/shortid": "^0.0.29",
"@types/use-subscription": "^1.0.0",
"del-cli": "^3.0.0",
"react": "~16.9.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/useRegisterNavigator.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as React from 'react';
import shortid from 'shortid';
import { nanoid } from 'nanoid';
import { SingleNavigatorContext } from './EnsureSingleNavigator';

/**
* Register a navigator in the parent context (either a navigation container or a screen).
* This is used to prevent multiple navigators under a single container or screen.
*/
export default function useRegisterNavigator() {
const [key] = React.useState(() => shortid());
const [key] = React.useState(() => nanoid());
const container = React.useContext(SingleNavigatorContext);

if (container === undefined) {
Expand Down
2 changes: 1 addition & 1 deletion packages/routers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"clean": "del lib"
},
"dependencies": {
"shortid": "^2.2.15"
"nanoid": "^3.0.2"
},
"devDependencies": {
"@react-native-community/bob": "^0.10.0",
Expand Down
6 changes: 2 additions & 4 deletions packages/routers/src/BaseRouter.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import shortid from 'shortid';
import { nanoid } from 'nanoid';
import { CommonNavigationAction, NavigationState, PartialState } from './types';

/**
Expand Down Expand Up @@ -55,9 +55,7 @@ const BaseRouter = {
return {
...nextState,
routes: nextState.routes.map((route) =>
route.key
? route
: { ...route, key: `${route.name}-${shortid()}` }
route.key ? route : { ...route, key: `${route.name}-${nanoid()}` }
),
};
}
Expand Down
6 changes: 3 additions & 3 deletions packages/routers/src/DrawerRouter.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import shortid from 'shortid';
import { nanoid } from 'nanoid';
import {
PartialState,
CommonNavigationAction,
Expand Down Expand Up @@ -115,7 +115,7 @@ export default function DrawerRouter(
...state,
stale: false,
type: 'drawer',
key: `drawer-${shortid()}`,
key: `drawer-${nanoid()}`,
};
},

Expand All @@ -136,7 +136,7 @@ export default function DrawerRouter(
return {
...state,
type: 'drawer',
key: `drawer-${shortid()}`,
key: `drawer-${nanoid()}`,
};
},

Expand Down
18 changes: 9 additions & 9 deletions packages/routers/src/StackRouter.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import shortid from 'shortid';
import { nanoid } from 'nanoid';
import BaseRouter from './BaseRouter';
import {
NavigationState,
Expand Down Expand Up @@ -113,12 +113,12 @@ export default function StackRouter(options: StackRouterOptions) {
return {
stale: false,
type: 'stack',
key: `stack-${shortid()}`,
key: `stack-${nanoid()}`,
index: 0,
routeNames,
routes: [
{
key: `${initialRouteName}-${shortid()}`,
key: `${initialRouteName}-${nanoid()}`,
name: initialRouteName,
params: routeParamList[initialRouteName],
},
Expand All @@ -139,7 +139,7 @@ export default function StackRouter(options: StackRouterOptions) {
(route) =>
({
...route,
key: route.key || `${route.name}-${shortid()}`,
key: route.key || `${route.name}-${nanoid()}`,
params:
routeParamList[route.name] !== undefined
? {
Expand All @@ -157,7 +157,7 @@ export default function StackRouter(options: StackRouterOptions) {
: routeNames[0];

routes.push({
key: `${initialRouteName}-${shortid()}`,
key: `${initialRouteName}-${nanoid()}`,
name: initialRouteName,
params: routeParamList[initialRouteName],
});
Expand All @@ -166,7 +166,7 @@ export default function StackRouter(options: StackRouterOptions) {
return {
stale: false,
type: 'stack',
key: `stack-${shortid()}`,
key: `stack-${nanoid()}`,
index: routes.length - 1,
routeNames,
routes,
Expand All @@ -186,7 +186,7 @@ export default function StackRouter(options: StackRouterOptions) {
: routeNames[0];

routes.push({
key: `${initialRouteName}-${shortid()}`,
key: `${initialRouteName}-${nanoid()}`,
name: initialRouteName,
params: routeParamList[initialRouteName],
});
Expand Down Expand Up @@ -238,7 +238,7 @@ export default function StackRouter(options: StackRouterOptions) {
routes: state.routes.map((route, i) =>
i === index
? {
key: key !== undefined ? key : `${name}-${shortid()}`,
key: key !== undefined ? key : `${name}-${nanoid()}`,
name,
params:
routeParamList[name] !== undefined
Expand All @@ -263,7 +263,7 @@ export default function StackRouter(options: StackRouterOptions) {
{
key:
action.payload.key === undefined
? `${action.payload.name}-${shortid()}`
? `${action.payload.name}-${nanoid()}`
: action.payload.key,
name: action.payload.name,
params:
Expand Down
12 changes: 6 additions & 6 deletions packages/routers/src/TabRouter.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import shortid from 'shortid';
import { nanoid } from 'nanoid';
import BaseRouter from './BaseRouter';
import {
NavigationState,
Expand Down Expand Up @@ -126,7 +126,7 @@ export default function TabRouter({

const routes = routeNames.map((name) => ({
name,
key: `${name}-${shortid()}`,
key: `${name}-${nanoid()}`,
params: routeParamList[name],
}));

Expand All @@ -135,7 +135,7 @@ export default function TabRouter({
return {
stale: false,
type: 'tab',
key: `tab-${shortid()}`,
key: `tab-${nanoid()}`,
index,
routeNames,
history,
Expand All @@ -161,7 +161,7 @@ export default function TabRouter({
key:
route && route.name === name && route.key
? route.key
: `${name}-${shortid()}`,
: `${name}-${nanoid()}`,
params:
routeParamList[name] !== undefined
? {
Expand Down Expand Up @@ -195,7 +195,7 @@ export default function TabRouter({
return {
stale: false,
type: 'tab',
key: `tab-${shortid()}`,
key: `tab-${nanoid()}`,
index,
routeNames,
history,
Expand All @@ -208,7 +208,7 @@ export default function TabRouter({
(name) =>
state.routes.find((r) => r.name === name) || {
name,
key: `${name}-${shortid()}`,
key: `${name}-${nanoid()}`,
params: routeParamList[name],
}
);
Expand Down
2 changes: 1 addition & 1 deletion packages/routers/src/__tests__/BaseRouter.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import BaseRouter from '../BaseRouter';
import * as CommonActions from '../CommonActions';

jest.mock('shortid', () => () => 'test');
jest.mock('nanoid', () => ({ nanoid: () => 'test' }));

const STATE = {
stale: false as const,
Expand Down
2 changes: 1 addition & 1 deletion packages/routers/src/__tests__/DrawerRouter.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
DrawerNavigationState,
} from '..';

jest.mock('shortid', () => () => 'test');
jest.mock('nanoid', () => ({ nanoid: () => 'test' }));

it('gets initial state from route names and params with initialRouteName', () => {
const router = DrawerRouter({ initialRouteName: 'baz' });
Expand Down
2 changes: 1 addition & 1 deletion packages/routers/src/__tests__/StackRouter.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CommonActions, StackRouter, StackActions } from '..';

jest.mock('shortid', () => () => 'test');
jest.mock('nanoid', () => ({ nanoid: () => 'test' }));

it('gets initial state from route names and params with initialRouteName', () => {
const router = StackRouter({ initialRouteName: 'baz' });
Expand Down
2 changes: 1 addition & 1 deletion packages/routers/src/__tests__/TabRouter.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CommonActions, TabRouter, TabActions, TabNavigationState } from '..';

jest.mock('shortid', () => () => 'test');
jest.mock('nanoid', () => ({ nanoid: () => 'test' }));

it('gets initial state from route names and params with initialRouteName', () => {
const router = TabRouter({ initialRouteName: 'baz' });
Expand Down
20 changes: 4 additions & 16 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3663,11 +3663,6 @@
"@types/glob" "*"
"@types/node" "*"

"@types/shortid@^0.0.29":
version "0.0.29"
resolved "https://registry.yarnpkg.com/@types/shortid/-/shortid-0.0.29.tgz#8093ee0416a6e2bf2aa6338109114b3fbffa0e9b"
integrity sha1-gJPuBBam4r8qpjOBCRFLP7/6Dps=

"@types/source-list-map@*":
version "0.1.2"
resolved "https://registry.yarnpkg.com/@types/source-list-map/-/source-list-map-0.1.2.tgz#0078836063ffaf17412349bba364087e0ac02ec9"
Expand Down Expand Up @@ -13131,10 +13126,10 @@ nan@^2.12.1, nan@^2.14.0:
resolved "https://registry.yarnpkg.com/nan/-/nan-2.14.0.tgz#7818f722027b2459a86f0295d434d1fc2336c52c"
integrity sha512-INOFj37C7k3AfaNTtX8RhsTw7qRy7eLET14cROi9+5HAVbbHuIWUHEauBv5qT4Av2tWasiTY1Jw6puUNqRJXQg==

nanoid@^2.1.0:
version "2.1.11"
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-2.1.11.tgz#ec24b8a758d591561531b4176a01e3ab4f0f0280"
integrity sha512-s/snB+WGm6uwi0WjsZdaVcuf3KJXlfGl2LcxgwkEwJF0D/BWzVWAZW/XY4bFaiR7s0Jk3FPvlnepg1H1b1UwlA==
nanoid@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.0.2.tgz#37e91e6f5277ce22335be473e2a5db1bd96dd026"
integrity sha512-WOjyy/xu3199NlQiQWlx7VbspSFlGtOxa1bRX9ebmXOnp1fje4bJfjPs1wLQ8jZbJUfD+yceJmw879ZSaVJkdQ==

nanomatch@^1.2.9:
version "1.2.13"
Expand Down Expand Up @@ -16521,13 +16516,6 @@ shellwords@^0.1.1:
resolved "https://registry.yarnpkg.com/shellwords/-/shellwords-0.1.1.tgz#d6b9181c1a48d397324c84871efbcfc73fc0654b"
integrity sha512-vFwSUfQvqybiICwZY5+DAWIPLKsWO31Q91JSKl3UYv+K5c2QRPzn0qzec6QPu1Qc9eHYItiP3NdJqNVqetYAww==

shortid@^2.2.15:
version "2.2.15"
resolved "https://registry.yarnpkg.com/shortid/-/shortid-2.2.15.tgz#2b902eaa93a69b11120373cd42a1f1fe4437c122"
integrity sha512-5EaCy2mx2Jgc/Fdn9uuDuNIIfWBpzY4XIlhoqtXF6qsf+/+SGZ+FxDdX/ZsMZiWupIWNqAEmiNY4RC+LSmCeOw==
dependencies:
nanoid "^2.1.0"

side-channel@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/side-channel/-/side-channel-1.0.2.tgz#df5d1abadb4e4bf4af1cd8852bf132d2f7876947"
Expand Down

0 comments on commit 9bb5cfd

Please sign in to comment.