OData V4 Literal Converter
The `odata-v4-literal` package is a focused utility designed to convert OData V4 literal values (represented as strings, e.g., `'Hello'`, `123`, `true`, `2023-01-15T10:30:00Z`) into their corresponding native JavaScript data types. This functionality is essential for applications that parse OData query parameters or expressions and need to process the actual values. The package is currently at version 0.1.1, and its last publish date was over nine years ago, indicating that it is effectively abandoned. There is no active development or defined release cadence. Its primary differentiator is providing a direct, simple method for handling the specific syntax and escaping rules of OData V4 primitive literals, which can be complex for types like strings (requiring double single quotes for embedded apostrophes), dates, and GUIDs. Developers should be aware of its unmaintained status when considering its use in new projects.
Common errors
-
Cannot read properties of undefined (reading 'convert') or similar TypeError
cause The `Literal` object was not correctly imported or accessed from the package's module export.fixEnsure you are using `import { Literal } from 'odata-v4-literal';` for ESM or `const { Literal } = require('odata-v4-literal');` for CommonJS to correctly destructure the named export. -
Error: Unrecognized literal format or Malformed OData literal
cause The input string does not conform to the expected OData V4 literal format for the specified EDM type.fixReview the OData V4 specification for the correct literal format for the EDM type you are trying to convert. Pay close attention to string delimiters (single quotes), date/time formats (ISO 8601), and special character escaping (e.g., doubling single quotes within a string literal).
Warnings
- breaking The package version is 0.1.1 and has not been updated in over nine years. There are no guarantees of semantic versioning (SemVer) compliance. Future (hypothetical) updates or even minor bug fixes could introduce breaking changes without a major version increment. However, given its abandoned status, no future updates are expected.
- gotcha OData V4 string literals require single quotes to be escaped by doubling them (e.g., 'O''Neill' for 'O'Neill'). Incorrectly escaping strings will lead to parsing errors or incorrect values. This is a common pitfall in OData filter expressions.
- gotcha This package is effectively abandoned, with its last publish occurring over nine years ago. This means there will be no future updates, bug fixes, security patches, or compatibility improvements for newer Node.js versions, TypeScript versions, or OData specification changes. Use with caution in new projects, as it may become incompatible or insecure over time.
Install
-
npm install odata-v4-literal -
yarn add odata-v4-literal -
pnpm add odata-v4-literal
Imports
- Literal
import Literal from 'odata-v4-literal';
import { Literal } from 'odata-v4-literal'; - Literal (CommonJS)
const Literal = require('odata-v4-literal');const { Literal } = require('odata-v4-literal');
Quickstart
import { Literal } from 'odata-v4-literal';
// Demonstrate conversion of various OData V4 literal types
console.log('--- OData V4 Literal Conversions ---');
// Edm.String (note the escaping for O'Neill)
const string1 = Literal.convert("Edm.String", "'Hello O''Neill'");
console.log(`'Hello O''Neill' (Edm.String) -> ${JSON.stringify(string1)} (Type: ${typeof string1})`);
// Another simple Edm.String
const string2 = Literal.convert("Edm.String", "'Another String'");
console.log(`'Another String' (Edm.String) -> ${JSON.stringify(string2)} (Type: ${typeof string2})`);
// Edm.Int32
const int32 = Literal.convert("Edm.Int32", "123");
console.log(`123 (Edm.Int32) -> ${JSON.stringify(int32)} (Type: ${typeof int32})`);
// Edm.Decimal / Edm.Double
const decimal = Literal.convert("Edm.Decimal", "123.45");
console.log(`123.45 (Edm.Decimal) -> ${JSON.stringify(decimal)} (Type: ${typeof decimal})`);
// Edm.Boolean
const booleanTrue = Literal.convert("Edm.Boolean", "true");
console.log(`true (Edm.Boolean) -> ${JSON.stringify(booleanTrue)} (Type: ${typeof booleanTrue})`);
const booleanFalse = Literal.convert("Edm.Boolean", "false");
console.log(`false (Edm.Boolean) -> ${JSON.stringify(booleanFalse)} (Type: ${typeof booleanFalse})`);
// Edm.Guid
const guid = Literal.convert("Edm.Guid", "80336209-66cb-4034-8b63-952467d344ff");
console.log(`80336209-66cb-4034-8b63-952467d344ff (Edm.Guid) -> ${JSON.stringify(guid)} (Type: ${typeof guid})`);
// Edm.DateTimeOffset (ISO 8601 format)
const dateTimeOffset = Literal.convert("Edm.DateTimeOffset", "2023-01-15T10:30:00Z");
console.log(`2023-01-15T10:30:00Z (Edm.DateTimeOffset) -> ${JSON.stringify(dateTimeOffset)} (Type: ${typeof dateTimeOffset})`);
// Edm.Date
const date = Literal.convert("Edm.Date", "2023-01-15");
console.log(`2023-01-15 (Edm.Date) -> ${JSON.stringify(date)} (Type: ${typeof date})`);
// Example of a potentially malformed literal (unquoted string for Edm.String)
try {
// This will likely throw an error or return null/undefined depending on implementation
const malformed = Literal.convert("Edm.String", "Unquoted String");
console.log(`'Unquoted String' (malformed Edm.String) -> ${JSON.stringify(malformed)}`);
} catch (e: any) {
console.error(`Error converting malformed string literal: ${e.message}`);
}