From ea163e8364d157212b5609929d87fcd5bed24ff2 Mon Sep 17 00:00:00 2001 From: Haixing Hu Date: Sun, 7 Apr 2024 18:20:11 +0800 Subject: [PATCH] fix: the NamingStyle cannot be freeze --- package.json | 10 +++--- src/index.js | 19 ++++++++---- src/naming-style.js | 27 +++++++---------- test/naming-style.immutable.test.js | 47 ++++++++++++++--------------- 4 files changed, 52 insertions(+), 51 deletions(-) diff --git a/package.json b/package.json index 13fa4b6..8790fde 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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": { diff --git a/src/index.js b/src/index.js index 2f6f62f..59829fe 100644 --- a/src/index.js +++ b/src/index.js @@ -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". @@ -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". @@ -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". @@ -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". @@ -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; diff --git a/src/naming-style.js b/src/naming-style.js index c2916ce..9b6010c 100644 --- a/src/naming-style.js +++ b/src/naming-style.js @@ -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 @@ -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 @@ -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 @@ -115,7 +115,7 @@ class NamingStyle { } return undefined; }, - ); + )); /** * Returns all the naming style constants. @@ -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; diff --git a/test/naming-style.immutable.test.js b/test/naming-style.immutable.test.js index 0c5edb2..3af2cc3 100644 --- a/test/naming-style.immutable.test.js +++ b/test/naming-style.immutable.test.js @@ -12,7 +12,6 @@ import { LOWER_CAMEL, UPPER_CAMEL, UPPER_UNDERSCORE, - NamingStyle, } from '../src'; /** @@ -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); + // }); });