Skip to content

Commit

Permalink
fix: don't use Object.fromEntries
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed May 16, 2020
1 parent c2aa4bb commit 51f4d11
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 43 deletions.
37 changes: 21 additions & 16 deletions packages/core/src/getPathFromState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ type StringifyConfig = Record<string, (value: any) => string>;

type OptionsItem = PathConfig[string];

type ConfigItem = {
pattern?: string;
stringify?: StringifyConfig;
screens?: Record<string, ConfigItem>;
};

/**
* Utility to serialize a navigation state object to a path string.
*
Expand Down Expand Up @@ -79,7 +85,7 @@ export default function getPathFromState(
if (route.params) {
const stringify = currentOptions[route.name]?.stringify;

currentParams = Object.fromEntries(
currentParams = fromEntries(
Object.entries(route.params).map(([key, value]) => [
key,
stringify?.[key] ? stringify[key](value) : String(value),
Expand Down Expand Up @@ -176,23 +182,23 @@ export default function getPathFromState(
return path;
}

type ConfigItem = {
pattern?: string;
stringify?: StringifyConfig;
screens?: Record<string, ConfigItem>;
};
// Object.fromEntries is not available in older iOS versions
const fromEntries = <K extends string, V>(entries: (readonly [K, V])[]) =>
entries.reduce((acc, [k, v]) => {
acc[k] = v;
return acc;
}, {} as Record<K, V>);

function joinPaths(...paths: string[]): string {
return ([] as string[])
const joinPaths = (...paths: string[]): string =>
([] as string[])
.concat(...paths.map((p) => p.split('/')))
.filter(Boolean)
.join('/');
}

function createConfigItem(
const createConfigItem = (
config: OptionsItem | string,
parentPattern?: string
): ConfigItem {
): ConfigItem => {
if (typeof config === 'string') {
// If a string is specified as the value of the key(e.g. Foo: '/path'), use it as the pattern
const pattern = parentPattern ? joinPaths(parentPattern, config) : config;
Expand All @@ -217,17 +223,16 @@ function createConfigItem(
stringify: config.stringify,
screens,
};
}
};

function createNormalizedConfigs(
const createNormalizedConfigs = (
options: PathConfig,
pattern?: string
): Record<string, ConfigItem> {
return Object.fromEntries(
): Record<string, ConfigItem> =>
fromEntries(
Object.entries(options).map(([name, c]) => {
const result = createConfigItem(c, pattern);

return [name, result];
})
);
}
54 changes: 27 additions & 27 deletions packages/core/src/getStateFromPath.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -223,20 +223,19 @@ export default function getStateFromPath(
return result;
}

function joinPaths(...paths: string[]): string {
return ([] as string[])
const joinPaths = (...paths: string[]): string =>
([] as string[])
.concat(...paths.map((p) => p.split('/')))
.filter(Boolean)
.join('/');
}

function createNormalizedConfigs(
const createNormalizedConfigs = (
screen: string,
routeConfig: PathConfig,
routeNames: string[] = [],
initials: InitialRouteConfig[],
parentPattern?: string
): RouteConfig[] {
): RouteConfig[] => {
const configs: RouteConfig[] = [];

routeNames.push(screen);
Expand Down Expand Up @@ -291,15 +290,15 @@ function createNormalizedConfigs(
routeNames.pop();

return configs;
}
};

function createConfigItem(
const createConfigItem = (
screen: string,
routeNames: string[],
pattern: string,
path: string,
parse?: ParseConfig
): RouteConfig {
): RouteConfig => {
// Normalize pattern to remove any leading, trailing slashes, duplicate slashes etc.
pattern = pattern.split('/').filter(Boolean).join('/');

Expand Down Expand Up @@ -327,25 +326,26 @@ function createConfigItem(
routeNames: [...routeNames],
parse,
};
}
};

function findParseConfigForRoute(
const findParseConfigForRoute = (
routeName: string,
flatConfig: RouteConfig[]
): ParseConfig | undefined {
): ParseConfig | undefined => {
for (const config of flatConfig) {
if (routeName === config.routeNames[config.routeNames.length - 1]) {
return config.parse;
}
}

return undefined;
}
};

// tries to find an initial route connected with the one passed
function findInitialRoute(
// Try to find an initial route connected with the one passed
const findInitialRoute = (
routeName: string,
initialRoutes: InitialRouteConfig[]
): string | undefined {
): string | undefined => {
for (const config of initialRoutes) {
if (config.connectedRoutes.includes(routeName)) {
return config.initialRouteName === routeName
Expand All @@ -354,16 +354,16 @@ function findInitialRoute(
}
}
return undefined;
}
};

// returns state object with values depending on whether
// it is the end of state and if there is initialRoute for this level
function createStateObject(
const createStateObject = (
initialRoute: string | undefined,
routeName: string,
params: Record<string, any> | undefined,
isEmpty: boolean
): InitialState {
): InitialState => {
if (isEmpty) {
if (initialRoute) {
return {
Expand All @@ -390,12 +390,12 @@ function createStateObject(
};
}
}
}
};

function createNestedStateObject(
const createNestedStateObject = (
routes: { name: string; params?: object }[],
initialRoutes: InitialRouteConfig[]
) {
) => {
let state: InitialState;
let route = routes.shift() as { name: string; params?: object };
let initialRoute = findInitialRoute(route.name, initialRoutes);
Expand Down Expand Up @@ -431,9 +431,9 @@ function createNestedStateObject(
}

return state;
}
};

function findFocusedRoute(state: InitialState) {
const findFocusedRoute = (state: InitialState) => {
let current: InitialState | undefined = state;

while (current?.routes[current.index || 0].state) {
Expand All @@ -446,12 +446,12 @@ function findFocusedRoute(state: InitialState) {
];

return route;
}
};

function parseQueryParams(
const parseQueryParams = (
path: string,
parseConfig?: Record<string, (value: string) => any>
) {
) => {
const query = path.split('?')[1];
const params = queryString.parse(query);

Expand All @@ -464,4 +464,4 @@ function parseQueryParams(
}

return Object.keys(params).length ? params : undefined;
}
};

0 comments on commit 51f4d11

Please sign in to comment.