Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor, extract sort-object into a new component #8649

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .bitmap
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,14 @@
"mainFile": "index.ts",
"rootDir": "scopes/ui-foundation/notifications/aspect"
},
"object/sorter": {
"name": "object/sorter",
"scope": "",
"version": "",
"defaultScope": "teambit.toolbox",
"mainFile": "index.ts",
"rootDir": "scopes/toolbox/object/sorter"
},
"overview/api-reference-table-of-contents": {
"name": "overview/api-reference-table-of-contents",
"scope": "teambit.api-reference",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { uniqWith } from 'lodash';
import { sortObject } from '@teambit/legacy/dist/utils';
import { sortObjectByKeys } from '@teambit/toolbox.object.sorter';
import { snapToSemver } from '@teambit/component-package-version';
import { Policy, SemverVersion, GitUrlVersion, FileSystemPath, PolicyConfigKeys } from '../policy';
import { KEY_NAME_BY_LIFECYCLE_TYPE, WorkspaceDependencyLifecycleType } from '../../dependencies';
Expand Down Expand Up @@ -132,10 +132,10 @@ export class WorkspacePolicy implements Policy<WorkspacePolicyConfigObject> {
return acc;
}, res);
if (res.dependencies) {
res.dependencies = sortObject(res.dependencies);
res.dependencies = sortObjectByKeys(res.dependencies);
}
if (res.peerDependencies) {
res.peerDependencies = sortObject(res.peerDependencies);
res.peerDependencies = sortObjectByKeys(res.peerDependencies);
}
return res;
}
Expand Down
1 change: 1 addition & 0 deletions scopes/toolbox/object/sorter/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { sortObjectByKeys } from './sort';
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Sort an object.
*/
export default function sortObject(obj: Record<string, any>) {
export function sortObjectByKeys(obj: Record<string, any>) {
return Object.keys(obj)
.sort()
.reduce(function (result, key) {
Expand Down
7 changes: 4 additions & 3 deletions src/consumer/bit-map/bit-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { LaneId } from '@teambit/lane-id';
import { BitError } from '@teambit/bit-error';
import { ComponentID, ComponentIdList } from '@teambit/component-id';
import { BitId, BitIdStr } from '@teambit/legacy-bit-id';
import { sortObjectByKeys } from '@teambit/toolbox.object.sorter';
import type { Consumer } from '..';
import {
AUTO_GENERATED_MSG,
Expand All @@ -18,7 +19,7 @@ import {
BITMAP_PREFIX_MESSAGE,
} from '../../constants';
import logger from '../../logger/logger';
import { pathJoinLinux, pathNormalizeToLinux, sortObject } from '../../utils';
import { pathJoinLinux, pathNormalizeToLinux } from '../../utils';
import { PathLinux, PathLinuxRelative, PathOsBased, PathOsBasedAbsolute, PathOsBasedRelative } from '../../utils/path';
import ComponentMap, {
ComponentMapFile,
Expand Down Expand Up @@ -148,7 +149,7 @@ export default class BitMap {
);
}
}
const sorted = sortObject(merged);
const sorted = sortObjectByKeys(merged);
// Delete and re-add it to make sure it will be at the end
delete sorted[SCHEMA_FIELD];
sorted[SCHEMA_FIELD] = parsed[SCHEMA_FIELD];
Expand Down Expand Up @@ -949,7 +950,7 @@ export default class BitMap {
components[key] = componentMapCloned.toPlainObject();
});

return sortObject(components);
return sortObjectByKeys(components);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/consumer/config/extension-data.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable max-classes-per-file */
import R from 'ramda';
import { ComponentID, ComponentIdList } from '@teambit/component-id';
import { sortObjectByKeys } from '@teambit/toolbox.object.sorter';
import { compact, isEmpty, cloneDeep } from 'lodash';
import { sortObject } from '../../utils';
import {
convertBuildArtifactsFromModelObject,
convertBuildArtifactsToModelObject,
Expand Down Expand Up @@ -226,7 +226,7 @@ export class ExtensionDataList extends Array<ExtensionDataEntry> {
const arr = R.sortBy(R.prop('stringId'), this);
// Also sort the config
arr.forEach((entry) => {
entry.config = sortObject(entry.config);
entry.config = sortObjectByKeys(entry.config);
});
return ExtensionDataList.fromArray(arr);
}
Expand Down
17 changes: 9 additions & 8 deletions src/consumer/consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import { Scope } from '../scope';
import { getAutoTagPending } from '../scope/component-ops/auto-tag';
import { ComponentNotFound } from '../scope/exceptions';
import { Lane, ModelComponent, Version } from '../scope/models';
import { generateRandomStr, sortObject } from '../utils';
import { generateRandomStr } from '../utils';
import { sortObjectByKeys } from '@teambit/toolbox.object.sorter';
import { composeComponentPath } from '../utils/bit/compose-component-path';
import {
PathAbsolute,
Expand Down Expand Up @@ -356,21 +357,21 @@ export default class Consumer {
componentFromModel.files = R.sortBy(R.prop('relativePath'), componentFromModel.files);
version.dependencies.sort();
version.devDependencies.sort();
version.packageDependencies = sortObject(version.packageDependencies);
version.devPackageDependencies = sortObject(version.devPackageDependencies);
version.peerPackageDependencies = sortObject(version.peerPackageDependencies);
version.packageDependencies = sortObjectByKeys(version.packageDependencies);
version.devPackageDependencies = sortObjectByKeys(version.devPackageDependencies);
version.peerPackageDependencies = sortObjectByKeys(version.peerPackageDependencies);
sortOverrides(version.overrides);
componentFromModel.dependencies.sort();
componentFromModel.devDependencies.sort();
componentFromModel.packageDependencies = sortObject(componentFromModel.packageDependencies);
componentFromModel.devPackageDependencies = sortObject(componentFromModel.devPackageDependencies);
componentFromModel.peerPackageDependencies = sortObject(componentFromModel.peerPackageDependencies);
componentFromModel.packageDependencies = sortObjectByKeys(componentFromModel.packageDependencies);
componentFromModel.devPackageDependencies = sortObjectByKeys(componentFromModel.devPackageDependencies);
componentFromModel.peerPackageDependencies = sortObjectByKeys(componentFromModel.peerPackageDependencies);
sortOverrides(componentFromModel.overrides);
}
function sortOverrides(overrides) {
if (!overrides) return;
DEPENDENCIES_FIELDS.forEach((field) => {
if (overrides[field]) overrides[field] = sortObject(overrides[field]);
if (overrides[field]) overrides[field] = sortObjectByKeys(overrides[field]);
});
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import mapToObject from './map/to-object';
import isNumeric from './number/is-numeric';
import cleanObject from './object-clean';
import objectToStringifiedTupleArray from './object-to-stringified-tuple-array';
import sortObject from './object/sort';
import resolveGroupId from './os-resolve-group-id';
import resolveHomePath from './os-resolve-home-path';
import { pathJoinLinux, pathNormalizeToLinux, pathRelativeLinux, pathResolveToLinux } from './path';
Expand All @@ -44,7 +43,6 @@ export {
objectToStringifiedTupleArray,
resolveGroupId,
mapToObject,
sortObject,
removeChalkCharacters,
getStringifyArgs,
isNumeric,
Expand Down