Skip to content

Commit

Permalink
fix: the NamingStyle cannot be freeze
Browse files Browse the repository at this point in the history
  • Loading branch information
Haixing-Hu committed Apr 7, 2024
1 parent 4c357cb commit ea163e8
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 51 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@haixing_hu/naming-style",
"version": "1.2.0",
"version": "1.3.0",
"description": "A JavaScript library for converting between different naming styles of identifiers.",
"author": "Haixing Hu",
"license": "Apache-2.0",
Expand All @@ -19,12 +19,12 @@
"conversion",
"naming style"
],
"main": "dist/naming-style.cjs.min.js",
"module": "dist/naming-style.esm.min.mjs",
"main": "dist/naming-style.cjs.js",
"module": "dist/naming-style.esm.mjs",
"exports": {
".": {
"require": "./dist/naming-style.cjs.min.js",
"import": "./dist/naming-style.esm.min.mjs"
"require": "./dist/naming-style.cjs.js",
"import": "./dist/naming-style.esm.mjs"
}
},
"scripts": {
Expand Down
19 changes: 13 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import NamingStyle from './naming-style';
*
* @type {NamingStyle}
*/
export const LOWER_HYPHEN = NamingStyle.LOWER_HYPHEN;
const LOWER_HYPHEN = NamingStyle.LOWER_HYPHEN;

/**
* C++/Python variable naming convention, e.g., "lower_underscore".
Expand All @@ -28,7 +28,7 @@ export const LOWER_HYPHEN = NamingStyle.LOWER_HYPHEN;
*
* @type {NamingStyle}
*/
export const LOWER_UNDERSCORE = NamingStyle.LOWER_UNDERSCORE;
const LOWER_UNDERSCORE = NamingStyle.LOWER_UNDERSCORE;

/**
* Java variable naming convention, e.g., "lowerCamel".
Expand All @@ -39,7 +39,7 @@ export const LOWER_UNDERSCORE = NamingStyle.LOWER_UNDERSCORE;
*
* @type {NamingStyle}
*/
export const LOWER_CAMEL = NamingStyle.LOWER_CAMEL;
const LOWER_CAMEL = NamingStyle.LOWER_CAMEL;

/**
* Java and C++ class naming convention, e.g., "UpperCamel".
Expand All @@ -50,7 +50,7 @@ export const LOWER_CAMEL = NamingStyle.LOWER_CAMEL;
*
* @type {NamingStyle}
*/
export const UPPER_CAMEL = NamingStyle.UPPER_CAMEL;
const UPPER_CAMEL = NamingStyle.UPPER_CAMEL;

/**
* Java and C++ constant naming convention, e.g., "UPPER_UNDERSCORE".
Expand All @@ -61,8 +61,15 @@ export const UPPER_CAMEL = NamingStyle.UPPER_CAMEL;
*
* @type {NamingStyle}
*/
export const UPPER_UNDERSCORE = NamingStyle.UPPER_UNDERSCORE;
const UPPER_UNDERSCORE = NamingStyle.UPPER_UNDERSCORE;

export { NamingStyle };
export {
NamingStyle,
LOWER_HYPHEN,
LOWER_UNDERSCORE,
LOWER_CAMEL,
UPPER_CAMEL,
UPPER_UNDERSCORE,
};

export default NamingStyle;
27 changes: 11 additions & 16 deletions src/naming-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class NamingStyle {
/**
* XML hyphenated variable naming style, e.g., `"lower-hyphen"`.
*/
static LOWER_HYPHEN = new NamingStyle(
static LOWER_HYPHEN = Object.freeze(new NamingStyle(
'lower-hyphen', // name
(c) => c === '-', // wordBoundaryFilter
'-', // wordSeparator
Expand All @@ -56,12 +56,12 @@ class NamingStyle {
}
return undefined;
},
);
));

/**
* C++/Python variable naming style, e.g., `"lower_underscore"`.
*/
static LOWER_UNDERSCORE = new NamingStyle(
static LOWER_UNDERSCORE = Object.freeze(new NamingStyle(
'lower-underscore', // name
(c) => c === '_', // wordBoundaryFilter
'_', // wordSeparator
Expand All @@ -75,33 +75,33 @@ class NamingStyle {
}
return undefined;
},
);
));

/**
* Java variable naming style, e.g., `"lowerCamel"`.
*/
static LOWER_CAMEL = new NamingStyle(
static LOWER_CAMEL = Object.freeze(new NamingStyle(
'lower-camel', // name
(c) => (c >= 'A' && c <= 'Z'), // wordBoundaryFilter
'', // wordSeparator
(w) => firstCharOnlyToUpper(w), // wordNormalizer
(w) => w.toLowerCase(), // firstWordNormalizer
);
));

/**
* Java and C++ class naming style, e.g., `"UpperCamel"`.
*/
static UPPER_CAMEL = new NamingStyle(
static UPPER_CAMEL = Object.freeze(new NamingStyle(
'upper-camel', // name
(c) => (c >= 'A' && c <= 'Z'), // wordBoundaryFilter
'', // wordSeparator
(w) => firstCharOnlyToUpper(w), // wordNormalizer
);
));

/**
* Java and C++ constant naming style, e.g., `"UPPER_UNDERSCORE"`.
*/
static UPPER_UNDERSCORE = new NamingStyle(
static UPPER_UNDERSCORE = Object.freeze(new NamingStyle(
'upper-underscore', // name
(c) => c === '_', // wordBoundaryFilter
'_', // wordSeparator
Expand All @@ -115,7 +115,7 @@ class NamingStyle {
}
return undefined;
},
);
));

/**
* Returns all the naming style constants.
Expand Down Expand Up @@ -248,11 +248,6 @@ class NamingStyle {
}

// freeze the constants and the class
Object.freeze(NamingStyle.LOWER_HYPHEN);
Object.freeze(NamingStyle.LOWER_UNDERSCORE);
Object.freeze(NamingStyle.LOWER_CAMEL);
Object.freeze(NamingStyle.UPPER_CAMEL);
Object.freeze(NamingStyle.UPPER_UNDERSCORE);
Object.freeze(NamingStyle);
// Object.freeze(NamingStyle);

export default NamingStyle;
47 changes: 23 additions & 24 deletions test/naming-style.immutable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
LOWER_CAMEL,
UPPER_CAMEL,
UPPER_UNDERSCORE,
NamingStyle,
} from '../src';

/**
Expand Down Expand Up @@ -121,27 +120,27 @@ describe('Test the immutability of the NamingStyle class', () => {
UPPER_UNDERSCORE.quickOptimizer = null;
}).toThrow(Error);
});
test('Test the immutability of NamingStyle class', () => {
expect(() => {
NamingStyle.LOWER_HYPHEN = 'xxx';
}).toThrow(Error);
expect(() => {
NamingStyle.LOWER_UNDERSCORE = null;
}).toThrow(Error);
expect(() => {
NamingStyle.LOWER_CAMEL = 'xx';
}).toThrow(Error);
expect(() => {
NamingStyle.UPPER_CAMEL = null;
}).toThrow(Error);
expect(() => {
NamingStyle.UPPER_UNDERSCORE = null;
}).toThrow(Error);
expect(() => {
NamingStyle.values = null;
}).toThrow(Error);
expect(() => {
NamingStyle.of = null;
}).toThrow(Error);
});
// test('Test the immutability of NamingStyle class', () => {
// expect(() => {
// NamingStyle.LOWER_HYPHEN = 'xxx';
// }).toThrow(Error);
// expect(() => {
// NamingStyle.LOWER_UNDERSCORE = null;
// }).toThrow(Error);
// expect(() => {
// NamingStyle.LOWER_CAMEL = 'xx';
// }).toThrow(Error);
// expect(() => {
// NamingStyle.UPPER_CAMEL = null;
// }).toThrow(Error);
// expect(() => {
// NamingStyle.UPPER_UNDERSCORE = null;
// }).toThrow(Error);
// expect(() => {
// NamingStyle.values = null;
// }).toThrow(Error);
// expect(() => {
// NamingStyle.of = null;
// }).toThrow(Error);
// });
});

0 comments on commit ea163e8

Please sign in to comment.