UCS-2 is a character encoding standard in which characters are represented by a fixed-length 16 bits (2 bytes)
With the encoding known you can decode properly and get the data as expected:
const data = [79, 0, 98, 0, 115, 0, 116, 0, 59, 0, 80, 0, 101, 0, 114, 0, 115, 0, 111, 0, 110, 0, 59, 0]
const dataBuffer = Buffer.from(data)
const utf16Decoder = new TextDecoder('UTF-16')
console.log(utf16Decoder.decode(dataBuffer)) // Obst;Person;
I think it's trying to decode as utf8 by default and getting upset at the unrenderable null bytes between every character:

node.js - Javascript - How to convert buffer to a string? - Stack Overflow
node.js - Convert string to buffer Node - Stack Overflow
angular - StringBuffer equivalent in typescript - Stack Overflow
Assigning Buffer to string issue
"utf8" should be given as an argument to fs.readFileSync, but you have it as an argument to path.join. Do this instead:
const configDataRaw = fs.readFileSync( path.join(__dirname, "../../../configuration.json"), "utf8" );
Providing an encoding argument to readFileSync (in this case the encoding is "utf8") causes TypeScript to apply an overloaded signature of readFileSync where the return type is string instead of Buffer. At runtime the encoding argument causes Node to convert the file content buffer to a string for you automatically.
// doesn't work
const configDataRaw = fs.readFileSync(
path.join(__dirname, "../../../configuration.json", "utf8")
);
const configData = JSON.parse(configDataRaw);
// const configDataRaw: Buffer
// Argument of type 'Buffer' is not assignable to parameter of type 'string'.ts(2345)
// works
const configData2 = JSON.parse(path.join(__dirname,
"../../../configuration.json", "utf8")
);All of the following failed:
const configDataRaw as string = fs.readFileSync(
const configDataRaw<string> = fs.readFileSync(
const configData = JSON.parse((configDataRaw as string));
const configData = JSON.parse(configDataRaw<string>);What is the correct way to assert this is not a buffer, but a string?
In the first code block, code sample #2, it seems to infer properly, I don't understand why the more iterative version acts differently here.
"utf8" should be given as an argument to fs.readFileSync, but you have it as an argument to path.join. Do this instead:
const configDataRaw = fs.readFileSync(
path.join(__dirname, "../../../configuration.json"),
"utf8"
);
Providing an encoding argument to readFileSync (in this case the encoding is "utf8") causes TypeScript to apply an overloaded signature of readFileSync where the return type is string instead of Buffer. At runtime the encoding argument causes Node to convert the file content buffer to a string for you automatically.
Type assertions go after the value.
If this is a JSON file, just use require or import.
Automatically converted when concatenated with an empty string:
console.log('' + bufferOne)
No native way for that, but I wrote a sample method for you:
function bufferFromBufferString(bufferStr) {
return Buffer.from(
bufferStr
.replace(/[<>]/g, '') // remove < > symbols from str
.split(' ') // create an array splitting it by space
.slice(1) // remove Buffer word from an array
.reduce((acc, val) =>
acc.concat(parseInt(val, 16)), []) // convert all strings of numbers to hex numbers
)
}
result:
const newBuffer = bufferFromBufferString('<Buffer 54 68 69 73 20 69 73 20 61 20 62 75 66 66 65 72 20 65 78 61 6d 70 6c 65 2e>')
> newBuffer
<Buffer 54 68 69 73 20 69 73 20 61 20 62 75 66 66 65 72 20 65 78 61 6d 70 6c 65 2e>
> newBuffer.toString()
'This is a buffer example.'
You can do:
var buf = Buffer.from(bufStr, 'utf8');
But this is a bit silly, so another suggestion would be to copy the minimal amount of code out of the called function to allow yourself access to the original buffer. This might be quite easy or fairly difficult depending on the details of that library.
You can use Buffer.from() to convert a string to buffer. More information on this can be found here
var buf = Buffer.from('some string', 'encoding');
for example
var buf = Buffer.from(bStr, 'utf-8');
I would use an array
@Pipe({ name: 'titleCase'})
class TitleCasePipe extends PipeTransform {
transform(value:string, args:any[] ):any {
buffer:string[] = [];
if ( !isNull( value ) ) {
list:string[] = value.split( '' );
if ( value.match(/^[is]{1,2}([A-Z]|[0-9]).*/ ) ) {
start:number = 2;
sublist:string[] = list.slice( start );
return sublist.join( '' );
}
for (i = 0; i < list.length; i++ ) {
if ( i == 0 ) {
buffer.push( list[i].toUpperCase( ) );
continue;
}
if ( isUppercase( list[i] ) ) {
buffer.push( ' ' );
buffer.push( list[i] );
continue;
}
buffer.push( list[i] );
}
}
return buffer.join('');
}
}
(caution - not tested)
Strings in TypeScript (and JavaScript) are mutable and iteratable.
var foo = "How Brown Cow";
> undefined
foo[2]
> "w"
In other languages where strings are immutable, you need a special StringBuffer or StringBuilder type to do concatenation or manipulation. With TypeScript you can do this with a simple string type.
That said, it looks like your titleCase pipe could be achieved with a simple regular expression rather than imperative code.
How to convert "camelCase" to "Camel Case"?
@Pipe('titleCase')
export class TitleCase implements PipeTransform {
transform(value: string | null) {
if (!value) return;
return value
.replace(/([A-Z])/g, ' $1')
.replace(/^./, s => s.toUpperCase());
}
}
It should be noted that Angular provides a titleCase pipe.