Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions lib/node_modules/@stdlib/proxy/ctor/lib/polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,39 @@
* var x = p.a;
* // returns 6.28
*/
function Proxy( target ) {
// TODO: polyfill implementation
return target;
function Proxy( target, handlers ) {
// Check if handlers and the 'get' trap function are provided
if (typeof handlers !== 'object' || typeof handlers.get !== 'function') {
throw new TypeError('Expected handlers to be an object with a "get" function.');
}

// Return a proxied object that intercepts property access
return new ProxyImpl(target, handlers);
}

/**
* Polyfill for the Proxy internals.
* This is the object that will mimic the behavior of the Proxy API.
*/
function ProxyImpl(target, handlers) {
this.target = target;
this.handlers = handlers;

return new Proxy(this, {
get: function(obj, prop) {
// Call the 'get' handler if defined
if (prop in handlers) {
return handlers[prop](target, prop);
}
// Otherwise, return the property from the target object
return target[prop];
},
set: function(obj, prop, value) {
target[prop] = value;
return true;
}
});
}

// EXPORTS //

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,69 +62,70 @@ var emojiProperty = grapheme.emojiProperty;
* var out = prevGraphemeClusterBreak( '🌷', 1 );
* // returns -1
*/
// cspell:ignore अनुच्छेद
function prevGraphemeClusterBreak( str, fromIndex ) {
var breaks;
var emoji;
var ans;
var len;
var idx;
var cp;
var i;

if ( !isString( str ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) );
}
len = str.length;
if ( arguments.length > 1 ) {
if ( !isInteger( fromIndex ) ) {
throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', fromIndex ) );
}
idx = fromIndex;
} else {
idx = len - 1;
}
if ( len === 0 || idx <= 0 ) {
return -1;
}
if ( idx >= len ) {
idx = len - 1;
}

// Initialize caches for storing grapheme break and emoji properties:
breaks = [];
emoji = [];

// Get the code point for the starting index:
cp = codePointAt( str, 0 );

// Get the corresponding grapheme break and emoji properties:
breaks.push( breakProperty( cp ) );
emoji.push( emojiProperty( cp ) );

ans = -1;
for ( i = 1; i <= idx; i++ ) {
// If the current character is part of a surrogate pair, move along...
if ( hasUTF16SurrogatePairAt( str, i-1 ) ) {
ans = i-2;
breaks.length = 0;
emoji.length = 0;
continue;
}
cp = codePointAt( str, i );

// Get the corresponding grapheme break and emoji properties:
breaks.push( breakProperty( cp ) );
emoji.push( emojiProperty( cp ) );

// Determine if we've encountered a grapheme cluster break...
if ( breakType( breaks, emoji ) > 0 ) {
ans = i-1;
breaks.length = 0;
emoji.length = 0;
continue;
}
}
return ans;
var breaks;
var emoji;
var ans;
var len;
var idx;
var cp;
var i;

if ( !isString( str ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) );
}
len = str.length;
if ( arguments.length > 1 ) {
if ( !isInteger( fromIndex ) ) {
throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', fromIndex ) );
}
idx = fromIndex;
} else {
idx = len - 1;
}
if ( len === 0 || idx <= 0 ) {
return -1;
}
if ( idx >= len ) {
idx = len - 1;
}

// Initialize caches for storing grapheme break and emoji properties:
breaks = [];
emoji = [];

// Get the code point for the starting index:
cp = codePointAt( str, 0 );

// Get the corresponding grapheme break and emoji properties:
breaks.push( breakProperty( cp ) );
emoji.push( emojiProperty( cp ) );

ans = -1;
for ( i = 1; i <= idx; i++ ) {
// If the current character is part of a surrogate pair, move along...
if ( hasUTF16SurrogatePairAt( str, i-1 ) ) {
ans = i-2;
breaks.length = 0;
emoji.length = 0;
continue;
}
cp = codePointAt( str, i );

// Get the corresponding grapheme break and emoji properties:
breaks.push( breakProperty( cp ) );
emoji.push( emojiProperty( cp ) );

// Determine if we've encountered a grapheme cluster break...
if ( breakType( breaks, emoji ) > 0 ) {
ans = i-1;
breaks.length = 0;
emoji.length = 0;
continue;
}
}
return ans;
}


Expand Down
15 changes: 15 additions & 0 deletions test/proxy-polyfill.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const Proxy = require('../lib/proxy/ctor/polyfill'); // Assuming the polyfill is inside the 'lib' folder

// Define a simple handler
const handler = {
get: (target, prop) => prop in target ? target[prop] * 2 : 0
};

// Create a proxied object
const obj = new Proxy({}, handler);

// Add a property
obj.a = 5;

// Check if the polyfill works
console.log(obj.a); // Should output 10, since it's doubled by the 'get' handler