Skip to content

Commit

Permalink
chore: removing the linear method and setting the recursive as the de…
Browse files Browse the repository at this point in the history
…fault
  • Loading branch information
Mhp23 committed Jan 12, 2025
1 parent 5c016e2 commit bb7c55c
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 117 deletions.
14 changes: 2 additions & 12 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,6 @@ createRStyle(styles, config);

The config options includes:

<h3>method</h3>

_Possible values_: `recursive` | `linear`

_Default_: `recursive`

To specify the parsing styles method, in this case, the recursive method is generally faster than the linear method (although the algorithm is not strictly linear, it has the potential for linear time complexity and is called linear). However, for deep and large structured objects, the linear algorithm may be more appropriate.

<h3>width</h3>

To use custom dimensions width for the calculation
Expand Down Expand Up @@ -156,11 +148,10 @@ const styles = createRStyle({

## useRStyle

A hook is provided for [createRStyle](#createrstyle) to create a dynamic responsive scale. This hook generates a new style when there are changes in dimensions, the parsing method, type, or bases. It accepts tree arguments:
A hook is provided for [createRStyle](#createrstyle) to create a dynamic responsive scale. This hook generates a new style when there are changes in dimensions, the parsing method, type, or bases. It accepts two arguments:

1. The first argument is the style and is required (as an object or a function)
2. The second argument is the dependency list to regenerate styles after changing them, and default is an empty array
3. The third argument is the parsing method which is optional and the default is `recursive`

An example:

Expand Down Expand Up @@ -191,8 +182,7 @@ const ResponsiveBox: React.FC = () => {
fontSize: `${SIZE}rs`,
},
},
[],
'recursive'
[]
);

return (
Expand Down
31 changes: 1 addition & 30 deletions __tests__/mapping.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import { rh, rs, rw } from '../src';
import { linearMapping } from '../src/layout/createRStyle/mapping/linearMapping';
import { recursiveMapping } from '../src/layout/createRStyle/mapping/recursiveMapping';

describe('linear and recursive mapping tests', () => {
describe('recursive mapping tests', () => {
it("should returns input value if it isn't an object or array", () => {
expect(linearMapping(undefined)).toEqual(undefined);
expect(linearMapping(null)).toEqual(null);
expect(linearMapping(123)).toEqual(123);
expect(linearMapping(true)).toEqual(true);
expect(linearMapping('style')).toEqual('style');
//Recursive Mapping
expect(recursiveMapping(undefined)).toEqual(undefined);
expect(recursiveMapping(null)).toEqual(null);
expect(recursiveMapping(123)).toEqual(123);
Expand All @@ -18,22 +11,6 @@ describe('linear and recursive mapping tests', () => {
});

it('should map empty object and nested objects correctly', () => {
expect(linearMapping({})).toEqual({});
expect(
linearMapping({
container: {},
text: {},
box: {
transoform: [],
},
})
).toEqual({
container: {},
text: {},
box: {
transoform: [],
},
});
expect(recursiveMapping({})).toEqual({});
expect(
recursiveMapping({
Expand Down Expand Up @@ -73,8 +50,6 @@ describe('linear and recursive mapping tests', () => {
height: rh(10),
},
};
expect(linearMapping(input)).toEqual(expectedResult);
//Recursive Mapping
expect(recursiveMapping(input)).toEqual(expectedResult);
});

Expand All @@ -84,8 +59,6 @@ describe('linear and recursive mapping tests', () => {
{ box: { width: rs(10) } },
{ box2: { width: rs(12.5) } },
];
expect(linearMapping(input)).toEqual(expectedResult);
//Recursive Mapping
expect(recursiveMapping(input)).toEqual(expectedResult);
});

Expand Down Expand Up @@ -136,8 +109,6 @@ describe('linear and recursive mapping tests', () => {
},
},
};
expect(linearMapping(input)).toEqual(expectedResult);
//Recursive Mapping
expect(recursiveMapping(input)).toEqual(expectedResult);
});
});
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
"@react-native-community/eslint-config": "^3.0.2",
"@release-it/conventional-changelog": "^5.0.0",
"@types/jest": "^28.1.2",
"@types/lodash": "^4.14.202",
"@types/react": "~17.0.21",
"@types/react-native": "0.70.0",
"commitlint": "^17.0.2",
Expand Down Expand Up @@ -172,7 +171,5 @@
"typescript"
]
},
"dependencies": {
"lodash": "^4.17.21"
}
"dependencies": {}
}
9 changes: 3 additions & 6 deletions src/hooks/useRStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,28 @@ import { type DependencyList, useMemo } from 'react';
import { useDevice } from './useDevice';
import { createRStyle } from '../layout';
import { useWindowDimensions } from 'react-native';
import type { MethodType, NamedStyles } from '../types';
import type { NamedStyles } from '../types';
/**
* A hook is provided for createRStyle to create a dynamic responsive scale. This hook generates a new style when there are changes in dimensions, the parsing method, type, or bases.
* @param styles
* @param deps dependency list to regenerate styles after changing them, and default is an empty array
* @param method parsing method which is optional and the default is `recursive`
* @returns a responsive styles `object`
*/
export const useRStyle = <T extends NamedStyles<T> | NamedStyles<any>>(
styles: T | (() => T),
deps: DependencyList = [],
method: MethodType = 'recursive'
deps: DependencyList = []
) => {
const device = useDevice();

const { width, height } = useWindowDimensions();

const dependencies = [width, height, method, device, ...deps];
const dependencies = [width, height, device, ...deps];

const responsivedStyles = useMemo(() => {
const passedStyles = typeof styles === 'function' ? styles() : styles;
return createRStyle(passedStyles, {
width,
height,
method,
scaleConfig: device,
});
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
12 changes: 5 additions & 7 deletions src/layout/createRStyle/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { StyleSheet } from 'react-native';
import { linearMapping } from './mapping/linearMapping';
import { recursiveMapping } from './mapping/recursiveMapping';
import type { StyleType, NamedStyles, CreateStyleConfig } from '../../types';
/**
Expand Down Expand Up @@ -27,10 +26,9 @@ export const createRStyle = <T extends NamedStyles<T> | NamedStyles<any>>(
style: T,
styleConfig?: Partial<CreateStyleConfig>
): StyleType<T> => {
const responsivedStyles =
styleConfig?.method === 'linear'
? linearMapping(style, styleConfig)
: recursiveMapping(style, styleConfig);
//@ts-ignore
return StyleSheet.create(responsivedStyles);
const responsivedStyles = recursiveMapping(
style,
styleConfig
) as StyleSheet.NamedStyles<any>;
return StyleSheet.create(responsivedStyles) as StyleType<T>;
};
45 changes: 0 additions & 45 deletions src/layout/createRStyle/mapping/linearMapping.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,7 @@ export type NamedStyles<T> = {
export type Pattern = 'rs' | 'rw' | 'rh';
export type ResponsivePattern = `${number}${Pattern}`;
export type ValuePattern = string | number | ResponsivePattern;
export type MethodType = 'linear' | 'recursive';
export type CreateStyleConfig = {
/**
* To specify the parsing styles method, in this case, the recursive method is generally faster than the linear
* method (although the algorithm is not strictly linear, it has the potential for linear time complexity and
* is called linear). However, for deep and large structured objects, the linear algorithm may be more appropriate.
* @default recursive
*/
method: MethodType;
/**
* To use custom dimensions width for the calculation
*/
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2668,11 +2668,6 @@
resolved "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.11.tgz"
integrity sha512-wOuvG1SN4Us4rez+tylwwwCV1psiNVOkJeM3AUWUNWg/jDQY2+HE/444y5gc+jBmRqASOm2Oeh5c1axHobwRKQ==

"@types/lodash@^4.14.202":
version "4.14.202"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.202.tgz#f09dbd2fb082d507178b2f2a5c7e74bd72ff98f8"
integrity sha512-OvlIYQK9tNneDlS0VN54LLd5uiPCBOp7gS5Z0f1mjoJYBrtStzgmJBxONW3U6OZqdtNzZPmn9BS/7WI7BFFcFQ==

"@types/minimist@^1.2.0", "@types/minimist@^1.2.2":
version "1.2.2"
resolved "https://registry.npmjs.org/@types/minimist/-/minimist-1.2.2.tgz"
Expand Down

0 comments on commit bb7c55c

Please sign in to comment.